dbt compile is throwing Object Not Found Error

The problem I’m having

dbt compile is failing with Object Not Found. The issue SQL lies under the IF EXECUTE block even then dbt compile is failing with error.

The context of why I’m trying to do this

I am curious if I am doing something wrong or there is a bug in dbt core

What I’ve already tried

Tried playing around with IF EXECUTE condition at multiple places but didn’t work

Some example code or error messages

Encountered an error: Runtime Error Database Error in model Test_Model (models/staging/views/Test_Model.sql) 002003 (02000): SQL compilation error: Schema ‘TEST_DB.TEST’ does not exist or not authorized.

The model is calling a Macro within.

Test_Mode.sql

{{ config(
          alias='TEST_MODEL'     
         ,schema='TEST'       
         ,materialized='view'                     
        )
}} 
-- depends_on: {{ ref('BASE_MODEL1') }}
-- depends_on: {{ ref('BASE_MODEL2') }}
{% if execute %}


{{ generate_view (  ref('BASE_MODEL1') , ref('BASE_MODEL2') , 'ARG1', 'TEST_TABLE' ) }}

{% endif %}

The Macro is using dbt_utils.get_single_value(sql_statement) to get some data from a table

{% macro generate_view(source,metadata_source,schema_name,table_name) %}

{% if execute %}

    {% set sql_statement %} 
       <SQL Statement>
       from {{ metadata_source }}
    {% endset %}

    {%- set gen_col_list = dbt_utils.get_single_value(sql_statement) -%}

    {% set ns = namespace(gen_col_list_modified=gen_col_list) %}      

    {%- for key, value in extract_columns.items() -%}  
        {%- set ns.gen_col_list_modified = ns.gen_col_list_modified | replace ( 'JSON:' + value['source'], key )  -%}
    {% endfor %}


    SELECT 
        {{ ns.gen_col_list_modified }} 
    FROM {{ source }}
    WHERE _CURRENT_RECORD = TRUE

{% endif %}

{% endmacro %}

I added if execute at multiple places to avoid this compilation when running dbt compile. But still it is trying to compile the sqls. If I run dbt RUN it will run everything since these dependent models will be created. But as part of deployment into upper env dbt compile is executed which will fail the deployment.

Any known issues with if execute statement?

dbt 1.5 version is being used.

1 Like

I am running into the exact same issue, did you get it figured out?