adapter.get_relation Not Working with BigQuery

The problem I’m having

adapter.get_relation cannot retrieve the relation for a table that exists in the BigQuery database.

The context of why I’m trying to do this

I’m following the dbt official course(1:00 - 3:00). The only difference is that my database is BigQuery, whereas the course uses Snowflake.
My code:

{% set old_relation = adapter.get_relation(
      database = target.database,
      schema = "dbt_xliu",
      identifier = "customer_orders"
) -%}

The error is:

Compilation Error in model test (models/test.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 model test (models/test.sql)

What I’ve already tried

All names are correct because the following code works:

{% set database = target.database %}
{% set schema = 'dbt_xliu' %}
{% set identifier = 'customer_orders' %}

FROM `{{ database }}.{{ schema }}.{{ identifier }}`

I also tried using {% if execute %}:

{% if execute %}
    {% set old_relation = adapter.get_relation(
          database = database_name,
          schema = schema_name,
          identifier = table_name
    ) %}
{% else %}
    {% set old_relation = [] %}
{% endif %}

but it does not work, because relation is till none and BigQuery compiles the code as “from ()”

I’m sure there is a BigQuery compiling problem here, but I can’t identify what it is.
Any suggestions would be greatly appreciated!

{% set old_relation = api.Relation.create(database =database_name,schema=schema_name, identifier=table_name) %}

Can you try this

It works, thank you!!
Do you know why my table exists in the database, but I have to create its relation rather than directly retrieving its relation?

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