Hi, at least in my case , this is because I’ve schema evolution enabled. If I don’t this is not a problem.
TLDR:
dbt will insert a dbt_unique_key column at their 2nd run of the model. You can notice this in the dbt.log file.
You will see how at the first run of an snapshot, dbt doesn’t use the dbt_unique_key column. But in the following snapshot runs it does. And due to the schema evolution enabled this causes the issue.
One workaround solution to support schema evolution in snapshot models is to set the following query in the post_hook section “ALTER TABLE {{ this }} DROP COLUMN IF EXISTS dbt_unique_key”
Have the same issue with AWS Athena. When using “dbt snapshot --build” you will see the sql where it fails.
dbt creates as t temporary table “…_tmp”
Error is in this part. The unique key get aliased with the name dbt_unique_key_1, while this column is also created in the snapshot table. Result is “AMBIGUOUS_NAME: line 48:35: Column ‘snapshotted_data.dbt_unique_key_1’ is ambiguou” :
snapshotted_data as (
select *,
model_name as dbt_unique_key_1
from "snapshot_tables_stg_count"
where
dbt_valid_to is null
)
Workarounds like a post_hook to drop a column does not work as this sql is generated by dbt to create a temporay table