Skip to content

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

@solomonshorser

Description

@solomonshorser

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 = {}

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestpaper_cutA small change that impacts lots of users in their day-to-dayvars

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions