adapter.get_relation compiling strangely

The problem I’m having

pre-reqs: We’re on dbt-core 1.3.3, using the latest release of every package we have.
The adapter.get_relation function seems to be compiling strangely. It looks to be compiling the relation of both the file being compiled and the relations specified within the file. This causes issues when attempting to use the audit_helper package, which is throwing the following error:

16:57:37  Encountered an error:
Compilation Error in analysis group_campaign_relation_audit (analysis/audits/group_campaign_relation_audit.sql)
  Macro get_filtered_columns_in_relation expected a Relation but received the value: None
  
  > in macro _is_relation (macros/jinja_helpers/_is_relation.sql)
  > called by macro default__get_filtered_columns_in_relation (macros/sql/get_filtered_columns_in_relation.sql)
  > called by macro get_filtered_columns_in_relation (macros/sql/get_filtered_columns_in_relation.sql)
  > called by macro compare_relations (macros/compare_relations.sql)
  > called by analysis group_campaign_relation_audit (analysis/audits/group_campaign_relation_audit.sql)

The context of why I’m trying to do this

I need to use that function for some of our audit_helper purposes.
It also seems pretty standard usage for dbt, so I’m curious why this behavior might be happening.

What I’ve already tried

I’ve tried testing the adapter.get_relation out, and it doesn’t seem to be functioning correctly.

Some example code or error messages

Here is the test model I’ve written within the file test_relations.sql

{% set source_relation = adapter.get_relation(
      database="healthjoy_bi",
      schema="analytics",
      identifier="group_campaign") -%}

{{ log("Source Relation: " ~ source_relation, info=true) }}

{% set dbt_relation=ref('group_campaign') %}

{{ log("dbt Relation: " ~ dbt_relation, info=true) }}

Here is the output after running the following command:
dbt compile --select test_relations

16:48:05  Running with dbt=1.3.3
16:48:06  Source Relation: None
16:48:06  dbt Relation: ANALYTICS.test_relations
16:48:06  Found 530 models, 322 tests, 14 snapshots, 0 analyses, 848 macros, 9 operations, 10 seed files, 233 sources, 15 exposures, 0 metrics
16:48:06  
16:48:08  Concurrency: 4 threads (target='dev')
16:48:08  
16:48:09  Source Relation: "HEALTHJOY_BI"."ANALYTICS"."GROUP_CAMPAIGN"
16:48:09  dbt Relation: ANALYTICS.group_campaign
16:48:09  Done.

We do override our ref function, which may be causing the issue. Here is how we do that:

{% macro ref(model_name) %}

  {% set rel = builtins.ref(model_name) %}

  {% if rel.database == 'HEALTHJOY_SNAPSHOTS' %}
    {% do return(builtins.ref(model_name)) %}
  {% else %}
    {% do return(builtins.ref(model_name).include(database=false)) %}
  {% endif %}

{% endmacro %}

I was having a similar problem when testing my update from 1.2 to 1.4. I found that that it was failing during the manifest construction step and if I wrapped my jina in and {% if execute %} block it resolved my problem.

1 Like

Thank you @pempey. I had been getting a parsing error with no further info and reached out to dbt support who didn’t really help me. Wrapping the audit_helper function in the {% if execute %} block solved it. Thanks so much.