I want to iterate over all the columns using dbt.
I think the star macro from the dbt-utils package + some for-loop logic might help you here? This depends on the exact use case and warehouse you're using (as pointed out in the comments).
The star macro generates a list of columns in the table provided.
So a possible approach would be something along the lines of:
{% for col in adapter.get_columns_in_relation(ref('my_model')) }}] %}
...operation...
{% endfor %}
{% for col in [ dbt_utils.star(ref('my_model')) ] %}
without curly brackets around the macro –
Jimmiejimmy dbt-core==1.7.13
and dbt-utils==1.1.1
: {% for col in adapter.get_columns_in_relation(ref("session_tables")) %}
. Mind the missing "{{" as Greg Hoar quoted. –
Sextet You can use the built-in adapter
wrapper and adapter.get_columns_in_relation:
{% for col in adapter.get_columns_in_relation(ref('<<your model>>')) -%}
... {{ col.column }} ...
{% endfor %}
I think the star macro from the dbt-utils package + some for-loop logic might help you here? This depends on the exact use case and warehouse you're using (as pointed out in the comments).
The star macro generates a list of columns in the table provided.
So a possible approach would be something along the lines of:
{% for col in adapter.get_columns_in_relation(ref('my_model')) }}] %}
...operation...
{% endfor %}
{% for col in [ dbt_utils.star(ref('my_model')) ] %}
without curly brackets around the macro –
Jimmiejimmy dbt-core==1.7.13
and dbt-utils==1.1.1
: {% for col in adapter.get_columns_in_relation(ref("session_tables")) %}
. Mind the missing "{{" as Greg Hoar quoted. –
Sextet If you have the model
node, and you have columns defined as model properties, this will work:
{% for col in model.columns.values() %}
... {{ col.name }} ... {{ col.data_type }} ...
{% endfor %}
You can get the model node from the graph:
{% set model = graph.nodes.values()
| selectattr("resource_type", "equalto", "model")
| selectattr("name", "equalto", model_name)
| first %}
© 2022 - 2024 — McMap. All rights reserved.