'Accessing a non-aliased table name relative to the graph node in DBT

I have a macro that gets me the literal table name of a dbt model. If I am wanting to get the table name of every dbt model in a project I can generally run this macro:

{% set table_names = [] %}

{% for node in graphs.nodes.values() | selectattr("resource_type", "equalto", "model") %}
  {{ table_names.append(node.config.alias) }}
{% endfor %}

But in the event that a model's config doesn't have an alias, the above code will append a None to the list. To solve for this problem we can add an if statement within the for loop like:

  {% if node.config.alias %}
    {{ table_names.append(node.config.alias) }}
  {% else %}
    {% set alias = node.unique_id %}
    {{ table_names.append(alias.split('.')[-1]) }}
  {% endif %}

So my ultimate: Is there a way to get the name of a non aliased model in dbt without having to do the else statement in the above?



Solution 1:[1]

I'm not 100% positive on this, but try node.alias instead of node.config.alias. Looks as if the manifest file puts either the alias or the model name there, depending on which one is being used to create the table/view name.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 anna