[CT-369] [Feature] Use variables declared in dbt_project.yml file in the file

See original GitHub issue

Is there an existing feature request for this?

  • I have searched the existing issues

Describe the Feature

When I declare a variable in a dbt_project file, I sometimes need to use it in the same file. For example:

vars:
  special_schema: "my_special_schema"

models:
  my_staging_models:
    staging:
      +materialized: view
      common:
        +enabled: true
        +schema: "{{ var('my_special_schema') }}"

special_schema is declared in the project-level vars because I need it in several places. I also need to specify this schema as the schema for some models. Currently when I do the above, I get an error:

Compilation Error Could not render {{ var(‘my_special_schema’) }}: Required var ‘my_special_schema’ not found in config: Vars supplied to <Configuration> = {}

Describe alternatives you’ve considered

Currently, I am adding a config block to every model to specify the schema, and that works, but with many models, it gets very repetitive. It would be nice to specify the schema once (using a variable), in the project file, and not have to do it again.

I suppose I could also include --vars "my_special_schema: some_schema" when running DBT, but for ease of use by others, I’d prefer to use the vars: block in the project file. We have many variables, and there are a few more I’d like to use in this way. Passing them all from the CLI makes for messier scripts.

Who will this benefit?

Everyone who wants to make use of variables in the project file, for variables that are declared in the project file.

Are you interested in contributing this feature?

Yes, but I’m not sure I have the necessary skills or time.

Anything else?

No response

Issue Analytics

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

github_iconTop GitHub Comments

2reactions
solomonshorsercommented, Apr 27, 2022

Having the vars in a separate file as one option could make life easier for some devops people as they could move a project around between environments and instead of modifying the vars in the project file, they could just swap different vars files in different environments and leave everything else untouched.

0reactions
github-actions[bot]commented, Dec 13, 2022

Although we are closing this issue as stale, it’s not gone forever. Issues can be reopened if there is renewed community interest. Just add a comment to notify the maintainers.

Read more comments on GitHub >

github_iconTop Results From Across the Web

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 >
Nested variables in dbt_project.yml file of dbt - Stack Overflow
You can reference nested variables using square bracket notation following the var('') command that references the top-most variable.
Read more >
Can you store variables in a custom YML file in your model?
This basically isn't possible without declaring a variable in dbt_project.yml. You can use environment variables or regular Jinja statements ...
Read more >
Add dbt project variables & use them in models - YouTube
In this video I will walk through the two ways that dbt allows you to define variables - In the dbt_project. yml file...
Read more >
Working with dbt - Continual Documentation
Continual feature sets and models can be defined by annotating dbt files with ... If using dbt config metadata in the dbt project...
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