How to reference a model that doesn't exist

I have a scenario where I’m rolling out some boilerplate code to multiple customers and I want to be able to reference a model that may or may not exist at a specific customer. I’m attempting to do something like the following:

    {% if ref("_test_stg__model2") is not none %}
    {% set model_to_use = "_test_stg__model2"%}
    {% else %}
    {% set model_to_use = "_test_stg__model1"%}
    {% endif %}

    select * from {{ref(model_to_use)}}

This returns an error because only model1 exists and model2 does not. I want this to execute model1 when model2 does not exist.

Has anybody done something similar to this before that can put me on the right track?

Thanks

Hey @nickbaker, I’ve had a little play around/test with something locally and the following seems to work:

{% if execute %}

    {% set possible_model = "_test_stg__model2" %}
    {% set default_model = "_test_stg__model1" %}

    {% if "model." ~ "<insert_project_name>." ~ possible_model in graph.nodes %}
        {% set selected_model = possible_model %}
    {% else %}
        {% set selected_model = default_model %}
    {% endif %}

select * from <database>.<schema>.{{selected_model}}

{% endif %}

Just replace the values in <> with your own relevant values.

Brilliant thank you! That gives me some challenges with database and schema name being hardcoded rather than using ref() but I think I can probably work around those.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.