diff --git a/macros/utils/graph/get_elementary_relation.sql b/macros/utils/graph/get_elementary_relation.sql index a50de55e0..76b99d65d 100644 --- a/macros/utils/graph/get_elementary_relation.sql +++ b/macros/utils/graph/get_elementary_relation.sql @@ -20,10 +20,32 @@ {% if this and this.database == elementary_database and this.schema == elementary_schema and this.identifier == identifier_alias %} {% do return(this) %} {% endif %} - {% do return( - adapter.get_relation( - elementary_database, elementary_schema, identifier_alias + {% set relation = adapter.get_relation( + elementary_database, elementary_schema, identifier_alias + ) %} + {% if relation is not none %} {% do return(relation) %} {% endif %} + {# Relation not found in the target schema. Under dbt deferral + (--favor-state / --defer) the Elementary models may exist only + in the deferred (e.g. prod) schema and not in the current + target. Construct a relation from the graph node coordinates + so the generated SQL references the correct schema instead of + rendering "from None". #} + {% set is_defer = ( + ( + invocation_args_dict.get("defer", false) + or invocation_args_dict.get("favor_state", false) ) + if invocation_args_dict + else false ) %} + {% if identifier_node and is_defer %} + {% do return( + api.Relation.create( + database=elementary_database, + schema=elementary_schema, + identifier=identifier_alias, + ) + ) %} + {% endif %} {%- endif %} {% endmacro %}