Reputation: 83
I want to iterate over all the columns using dbt.
Upvotes: 7
Views: 10070
Reputation: 528
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 %}
Upvotes: 2
Reputation: 485
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 %}
Upvotes: 2
Reputation: 2927
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 %}
Upvotes: 8