The problem I’m having
I am using a macro to get a list of models that have a certain tag:
{% macro get_models_by_tag(tag) %}
{% if execute %}
{% set models_with_tag = [] %}
{% for model in graph.nodes.values() | selectattr("resource_type", "equalto", "model") %}
{% if tag in model.config.tags %}
{{ models_with_tag.append(model.name) }}
{% endif %}
{% endfor %}
{{ return(models_with_tag) }}
{% endif %}
{% endmacro %}
macro borrowed from this topic: Getting "'dict object' has no attribute 'nodes'" error trying to use graph.nodes.values()
But when I try to use this macro in a different model
{% set models = get_models_by_tag(tag='my_tag') %}
{% for model in models %}
select * from {{ ref( model ) }}
{% if not loop.last %} union all {% endif %}
{% endfor %}
I get:
Model 'xyz' (models/xyz/xyz.sql) depends on a node named '
' which was not found
What I’ve already tried
I tried encapsulating my model with {% if execute %} {% endif %} and then I get a different error:
Compilation Error in model xyz (models/xyz/xyz.sql)
dbt was unable to infer all dependencies for the model "xyz".
This typically happens when ref() is placed within a conditional block.
To fix this, add the following hint to the top of the model "xyz":
-- depends_on: {{ ref('zyx') }}
When I add the line it suggests, then it works, but that’s not really what I am looking for, as I am going to have a lot of models, and I don’t really want to have a lot of depends_on lines.
Please let me know if I am missing something, can this be done without depends_on line?