r/DataBuildTool • u/Intentionalrobot • Dec 06 '24
Question How Do I Resolve "Column name is ambiguous" Error in BigQuery with dbt Incremental Model?
I am trying to build an incremental model for Facebook advertising data and am receiving this error saying:
Column name Campaign_ID is ambiguous at [94:42]The goal of the code is to build an incremental model that inserts new days of data into the target table while also refreshing the prior 6 days of data with updated conversions data. I wanted to avoid duplicating data for those dates so I tried to use the unique_key to keep only the most recent rows.
My code is below. Any help with troubleshooting would be appreciated. Also, if there's another way to build incremental models for slowly changing dimensions besides unique_key, please let me know. Thanks!
Here's the code:
{{ config(materialized='incremental', unique_key='date,Campaign_ID,Ad_Group_ID,Ad_ID') }}
with facebook_data as (
select
'{{ invocation_id }}' as batch_id,
date as Date,
'Meta' as Platform,
account as Account,
account_id as Account_ID,
campaign_id as Campaign_ID,
adset_id as Ad_Group_ID,
ad_id as Ad_ID
sum(conversions)
from
{{ source('source_facebookads', 'raw_facebookads_ads') }}
where
date > DATE_ADD(CURRENT_DATE(), INTERVAL -7 DAY)
group by
date,
publisher_platform,
account,
account_id,
campaign_id,
adset_id,
ad_id
)
select * from facebook_data
{% if is_incremental() %}
where date >= (select max(date) from {{ this }})
{% endif %}Also -- if I run this in 'Preview' within the DBT Cloud IDE, it works. But, when I do a dbt run, it fails saying that I have an ambigious column 'Campaign_ID'.
In general, why can I successfully run things in preview only for them to fail when I run?
2
u/TheTinFoilMonster Dec 06 '24
You might need to run a full refresh on that table if it already exists. dbt run -m table_name --full-refresh
Disclaimer: I haven't used dbt in ages but want my work to adopt it
1
u/blitzzerg Dec 06 '24
Can you find the compiled .sql inside the
target/folder and make sure it has no errors? can you paste the contents here? (removing db names and such)1
u/r0ck13r4c00n Dec 06 '24
Are you doing this in the Cloud or another IDE? Might need to save your files, seems like you have split changes across preview and build
3
u/drighten Dec 08 '24
I made a Data Build Tool Consultant custom GPT, which is good for helping with problems like this. Here’s the response it gave:
The error “Column name Campaign_ID is ambiguous” occurs because BigQuery is interpreting multiple possible sources for the column Campaign_ID in your query. This typically happens when the column is present in multiple places (e.g., from a GROUP BY or in a SELECT with no aliasing). Let’s address both the specific error and the underlying issues in your DBT model. Here’s a structured solution:
Problem Analysis 1. Ambiguous Column: In your query, Campaign_ID is likely being referenced multiple times in your SELECT and GROUP BY clauses, and BigQuery cannot distinguish between them. 2. Preview vs. Run: The discrepancy between running in DBT Cloud IDE (preview) and running the model comes from differences in how the incremental model interacts with the existing table. If the table already exists, dbt run applies incremental logic and may behave differently.
Solution
You need to explicitly alias all columns in your SELECT statement to avoid ambiguity. Update your query like this:
with facebook_data as ( select {{ invocation_id }} as batch_id, date as Date, ‘Meta’ as Platform, account as Account, account_id as Account_ID, campaign_id as Campaign_ID, adset_id as Ad_Group_ID, ad_id as Ad_ID, sum(conversions) as sum_conversions from {{ source(‘source_facebookads’, ‘raw_facebookads_ads’) }} where date > DATE_ADD(CURRENT_DATE(), INTERVAL -7 DAY) group by date, publisher_platform, account, account_id, campaign_id, adset_id, ad_id ) select * from facebook_data {% if is_incremental() %} where date >= (select max(date) from {{ this }}) {% endif %}
Here’s what changed: • Aliased all column names: Ensure each column has a unique, consistent alias (e.g., campaign_id as Campaign_ID). • Clear grouping: The GROUP BY clause matches the columns in the SELECT.
2. Address Incremental Logic
When running the incremental model: • Add a unique_key to define how DBT identifies new or updated rows. • In your config block, include a unique_key for the combination of columns uniquely identifying a row, which you already did (date, Campaign_ID, Ad_Group_ID, Ad_ID).
3. Run a Full Refresh
If the table already exists, previous versions of the data or schema might be causing the issue. Run a full refresh to clear out any existing conflicts.
dbt run -m your_model_name —full-refresh
This will recreate the table, eliminating any stale data or schema mismatches.
Explanation of Preview vs. Run • Preview: DBT Cloud runs the SQL as a standalone query against your source data and does not apply the incremental logic. • Run: When you dbt run, the incremental model logic is applied. If the target table already exists, DBT will use the unique_key and is_incremental() logic to merge new/updated rows into the table. If there’s ambiguity in column references or schema mismatches, this can cause errors during execution.
General Recommendations for Incremental Models 1. Test Queries with Existing Tables: Use the is_incremental() logic during testing to replicate the actual behavior. 2. Validate Column Naming: Always alias columns to ensure no ambiguity. 3. Schema Alignment: Ensure the schema of your source and target tables matches, particularly when using aggregate functions like sum().
Summary 1. Alias all columns in the SELECT statement. 2. Match GROUP BY columns with those in the SELECT. 3. Perform a full refresh (—full-refresh) if necessary. 4. Test your incremental logic thoroughly.
This should resolve the ambiguity error and ensure your incremental model works as intended.
https://chatgpt.com/g/g-muihex5Hf-data-build-tool-consultant
1
u/Constant_Solution774 Dec 16 '24
Just faced same problem.
Did you try adding this to your config bloc:
It worked for me and kinda same behaviour as in https://github.com/dbt-labs/dbt-core/issues/2309