Using Variables in Get_Columns_In_Relation Dbt Function

I'm fairly new to macros and variables in dbt so apologize if this is really straightforward but I'm attempting the below but it's not recognizing the {{customer_dataset}}.{{table_name}} part

{%- set table_name = 'orders' -%}

{%- set customer_dataset = customer.id -%}

{%- set columns = adapter.get_columns_in_relation(`{{customer_dataset}}.{{table_name}}`) -%}

How do I do this properly if I want to dynamically loop through a bunch of tables for the get_columns_in_relation function?

1 Answer

First off, don't nest your curlies.

Second, get_columns_in_relation expects a Relation, not a string. A Relation is the thing returned by {{ ref() }} or {{ source() }}, so it's easiest to get a Relation from a model name, not a database identifier. If you want to use a table in your database that isn't a dbt model, create a source for it first.

In short, your code should probably look like this:

{%- set columns = adapter.get_columns_in_relation(source(customer_dataset, table_name)) -%}
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Sarah Jenkins

Sarah Jenkins

Senior Technology Editor & AI Specialist

Sarah Jenkins is a veteran tech journalist with over 12 years of experience covering artificial intelligence, mobile innovations, and digital ethics. Her insights have appeared in leading technology publications worldwide.

Share this article
Twitter Facebook Pinterest