Search code examples
jinja2dbt

Unable to use Jinja variable inside adapter.get_relation in DBT


I am trying to use a Jinja variable inside my adapter.get_relation but I am unable to do so :confused: The code


{% set the_var = 'amazon_full_orders_denormalized_' ~ company_uuid %}

{{ log(the_var, info=True) }}

{%- set source_relation = adapter.get_relation(
      database='282615',
      schema='airbyte_mumbai',
      identifier= the_var ) -%} 



The command I am using

dbt compile --vars '{"company_uuid":"0703afd3_496b_4ed5_8e0c_594b71f4718b","dataset":"airbyte_mumbai"}' --models tag:copy_reports

The variable the_var is getting set to amazon_full_orders_denormalized_

Found 6 models, 0 tests, 0 snapshots, 0 analyses, 165 macros, 0 operations, 0 seed files, 0 sources, 0 exposures

17:11:08 | Concurrency: 3 threads (target='dev')
17:11:08 | 
amazon_full_orders_denormalized_
Table does not exist
17:11:08 | Done.

Whereas I am expecting it to be set to amazon_full_orders_denormalized_0703afd3_496b_4ed5_8e0c_594b71f4718b


Solution

  • Using variables in dbt can be hard sometimes! I think the commenter @Kay is on the right track here in that you have three variables happening here: the_var, company_uuid, and dataset. It looks as if you'd like the a table name that is the concatenation of the_var and company_uuid, which you can do using jinja's concat operators, ~, like this:

    -- just for testing
    {{ log(the_var, info=True) }}
    {{ log(var('company_uuid'), info=True) }}
    {{ log(the_var ~ var('company_uuid'), info=True) }}
    
    -- see change to `identifier`
    {%- set source_relation = adapter.get_relation(
          database='282615',
          schema='airbyte_mumbai',
          identifier= the_var ~ var('company_uuid') ) -%}