Evaluation of `{{this}}`

I came across an interesting “feature” of dbt today that was not noticed until we got to Production.

If you configure a model like this:

{%- set post_hook_sql -%}
delete from {{ this }}
where stuff
{%- endset -%}

{{
    config(
        materialized="incremental",
        unique_key="snapshot_key",
        full_refresh=false,
        post_hook=[post_hook_sql ]
    )
}}

dbt is not fully aware of your schema configuration at the time the jinja variable is set. It will resolve to default schema instead of custom schema.

In order to get the schema name to resolve correctly (not use default schema), you need to call {{this}} in the config block instead. For example, this resolves to the correct schema:

{{
    config(
        materialized="incremental",
        unique_key="snapshot_key",
        full_refresh=false,
        post_hook=["delete from {{this}} where stuff"]
    )
}}

Questions:

  • Why?
  • Is this on purpose or is it some side effect?
  • Is it worth creating an issue on GitHub?

Not sure if it helps. But I guess it is worth trying

{% if execute %}
{%- set post_hook_sql -%}
delete from {{ this }}
where stuff
{%- endset -%}
{% endif %}