'None' has no attribute 'table'

I’m trying to create a macro to do some checks before performing a calculation with the field. but when checking the return of the qury I can’t, it gives an error.
How can I do this?
Follows my macro in a generic way

{% macro fn_verifica_id(id, ds_campo_pesquisar) %}

        {% set query %}
            select
                1 as resultado
        {% endset %}

        {% set resultado = run_query(query) %}

        {% if resultado == 1 %}
            ({{id}} + 1000 * (-1))
        {% else %}
            ({{id}})
        {% endif %}

{% endmacro %}

Hi, I’ve come across this exact error before. Basically, dbt will first parse the macro prior to execution. In this parsing stage, you get the error 'None' has no attribute 'table'.

To avoid this, you can wrap your macro in an if block to check whether dbt is in parsing or execution mode, and then write some valid but useless SQL for the parsing stage.

I’ve tested this solution and it works for me. So what you can do is

{% macro fn_verifica_id(id, ds_campo_pesquisar) %}

{% if execute %}

        {% set query %}
            select
                1 as resultado
        {% endset %}

        {% set resultado = run_query(query) %}

        {% if resultado == 1 %}
            ({{id}} + 1000 * (-1))
        {% else %}
            ({{id}})
        {% endif %}

{% endif %}

select 1 -- dummy SQL for parsing stage

{% endmacro %}

Hope it works!

3 Likes

Supporting documentation: https://docs.getdbt.com/reference/dbt-jinja-functions/execute

Note: @Owen originally posted this reply in Slack. It might not have transferred perfectly.

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