Does dbt defer work with a model that has a {{source()}} dependency?

The problem I’m having

Using defer fails when a model selects from a {{source()}} function in the model definition. ChatGPT says that defer won’t work with sources since they are not dbt models. However, I couldn’t find anything in the official dbt documentation to verify this.

Could someone confirm if this is the case or not?

If so, are there any common workarounds for this or do sources need to be turned into models?

The context of why I’m trying to do this

For UAT testing in order to use production data for testing.

What I have done so far

  • dbt docs search
  • internet search
  • StackOverflow search
  • Asked ChatGPT

Example Model Code

SELECT *
FROM {{source(some_source)}}

Example error messages

dbt run --models model_that_uses_source --defer  --state prod-run-artifacts

Runtime Error in model model_that_uses_source (models/dim/model_that_uses_source.sql)
  404 Not found: Dataset my_gcp_project:my_schema was not found in location US;

You’re correct that you can’t use deferral with sources. However, you could specify different schemas for UAT data vs prod data by specifying an environment variable and using that to indicate the location of your sources:

version: 2

sources:
  - name: jaffle_shop
    database: raw  
    schema: "{{ ENV_VAR('DBT_JAFFLE_SHOP_SCHEMA_LOCATION') }}"
    tables:
      - name: orders
      - name: customers

Then you can set the DBT_JAFFLE_SHOP_SCHEMA_LOCATION env var to jaffle_shop_prod or jaffle_shop_uat as appropriate for each environment.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.