question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

target-based Jinja rendering of vars in dbt_project.yml

See original GitHub issue

Describe the bug

When compiling / running models from dbt Cloud, the compiler does not correctly evaluate variables defined using jinja in dbt_project.yml and referenced in a sources: block.

Steps To Reproduce

Create in dbt_project.yml:

vars:
  database_prefix: "{% if target.name == 'dev' %}dev_{% else %}{% endif %}"

Create in sources.yml:

sources:
  - name: tpch
    database: "{{ var('database_prefix') }}raw"

Expected behavior

SQL compilation will resolve the variable to either dev_ or an empty string when running either dbt compile or dbt run

Screenshots and log output

On dbt compile: image

System information

Which database are you using dbt with?

  • postgres
  • redshift
  • bigquery
  • snowflake
  • other (specify: ____________)

The output of dbt --version:

0.18.1

The operating system you’re using: dbt Cloud

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:3
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
jtcohen6commented, Feb 16, 2021

Thanks for the bug report @matt-winkler! I get that this is unexpected behavior. The truth is that vars in dbt_project.yml aren’t Jinja-rendered today. dbt needs to know the values of vars before it can render any other parts of the project. This leads to weirdness today whereby var() is in the scope of the dbt_project.yml context, but only if set by --vars (docs).

Ultimately, a solution here would be to clearly establish the parsing order of project files:

  • env vars, --vars, dbt_version, and builtins are always available
  • parse profile --> get target values
  • parse vars (with env vars, --vars, and target available)
  • parse the rest of the project file

I’m definitely interested in rationalizing when vars are available, and what’s available to them: see https://github.com/fishtown-analytics/dbt/issues/2955. I don’t feel a ton of immediate priority here, though; in the use case you’re describing, there are several other ways to achieve this env-based behavior:

  • Your deployment can override {{ var('database_prefix') }} with a (static string) value passed to --vars
  • You could control this behavior via {{ env_var() }} instead

If you do want dbt to handle the database prefixing on the basis of target.name alone, it’s not possible to get any DRYer than this:

version: 2

sources:
  - name: tpch
    database: "{{ 'dev_' if target.name == 'dev' else '' }}raw"
    tables:
      - name: something
0reactions
github-actions[bot]commented, Nov 24, 2021

This issue has been marked as Stale because it has been open for 180 days with no activity. If you would like the issue to remain open, please remove the stale label or comment on the issue, or it will be closed in 7 days.

Read more comments on GitHub >

github_iconTop Results From Across the Web

target-based Jinja rendering of vars in dbt_project.yml #3105
When compiling / running models from dbt Cloud, the compiler does not correctly evaluate variables defined using jinja in dbt_project.yml and ...
Read more >
dbt_project.yml Context | dbt Developer Hub
The following context variables and methods are available when configuring resources in the dbt_project.yml file.
Read more >
Template Designer Documentation - Jinja
A template contains variables and/or expressions, which get replaced with values when a template is rendered; and tags, which control the logic of...
Read more >
Jinja2 Tutorial - Part 6 - Include and Import |
Jinja2 Tutorial - Part 1 - Introduction and variable substitution ... Jinja then inserts rendered text into the current template.
Read more >
How to Write dbt Macros Using Jinja2 - Towards Data Science
In order to do this in Jinja simply plug in the variable you wish to print to ... models to be whatever is...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found