Search code examples
pythonetldbt

macros are not recognised in dbt


{{ 
    config (
        pre_hook = before_begin("{{audit_tbl_insert(1,'stg_news_sentiment_analysis_incr') }}"),
        post_hook = after_commit("{{audit_tbl_update(1,'stg_news_sentiment_analysis_incr','dbt_development','news_sentiment_analysis') }}")
        )
}}

select rd.news_id ,rd.title, rd.description, ns.sentiment from live_crawler_output_rss.rss_data rd 
left join 
live_crawler_output_rss.news_sentiment ns 
on rd.news_id = ns.data_id limit 10000;

This is my model in DBT which is configured with pre and post hooks which referance a macro to insert and update the audit table.

my macro

{ % macro audit_tbl_insert (model_id_no, model_name_txt) % }

{% set run_id_value = var('run_id') %}

insert into {{audit_schema_name}}.{{audit_table_name}} (run_id, model_id, model_name, status, start_time, last_updated_at)
values 
({{run_id_value}}::bigint,{{model_id_no}}::bigint,{{model_name_txt}},'STARTED',current_timestamp,current_timestamp)

{% endmacro %}

this is the first time i'm using this macro and I see the following error.

Compilation Error in model stg_news_sentiment_analysis_incr 
(models/staging/stg_news_sentiment_analysis_incr.sql)
'audit_tbl_insert' is undefined in macro run_hooks (macros/materializations/hooks.sql) 
called by macro materialization_table_default (macros/materializations/models/table/table.sql) called by model stg_news_sentiment_analysis_incr 
(models/staging/stg_news_sentiment_analysis_incr.sql). 
This can happen when calling a macro that does not exist. 
Check for typos and/or install package dependencies with "dbt deps".

Solution

  • Your macro's definition has too much whitespace in the braces that define the jinja block:

    { % macro audit_tbl_insert (model_id_no, model_name_txt) % }
    

    Needs to be

    {% macro audit_tbl_insert (model_id_no, model_name_txt) %}
    

    And then this should work just fine.