Home > Net >  With dbt. How do I use a jinja macro in a yaml file
With dbt. How do I use a jinja macro in a yaml file

Time:12-21

I have a yaml file like this:

models:
       - name: test_view
        description: "test"
        config:
          meta:
            database_tags:
              ACCOUNT_OBJECTS.TAGS.ENV: DEV`


I am trying automatically change 'DEV' to PROD when it's in that environment. I have a macro that gets the variable from targets.name

This is the jinja code:

{% macro test_macro(target) %}
      {%- if  target.name == "dev" -%} DEV
      {%- elif target.name == "prod"  -%} PROD
      {%- else -%} invalid
      {%- endif -%}
    {% endmacro %}`

However, when I try to use the macro I get 'test_macro is undefined'

eg. ACCOUNT_OBJECTS.TAGS.ENV: {{ test_macro(target)}}

Is it that custom macros still cannot be used in yaml files?

CodePudding user response:

Macros are for templating the SQL queries DBT compiles from its models. The YAML files are for configuration; they are not themselves models and do not support macros.

I went looking, and there is an active discussion about whether this could be supported in future, but as of the end of 2022 it is not possible.

Have you considered using a config block inside your model in order to set these metadata?

  • Related