We use a unique key to tell dbt how to match rows when updating or inserting data. This helps keep data clean and avoid duplicates.
Unique key for merge behavior in dbt
unique_key: 'column_name' or unique_key: ['column1', 'column2']
The unique_key can be a single column or a list of columns that together identify each row uniquely.
This key is used in incremental models to decide which rows to update or insert.
id as the unique key for merging rows.unique_key: 'id'user_id and order_date as the unique key to match rows.unique_key: ['user_id', 'order_date']
This dbt model uses order_id as the unique key. When running incrementally, dbt will update rows with matching order_id and insert new rows.
{{ config(
materialized='incremental',
unique_key='order_id'
)}}
select
order_id,
user_id,
order_date,
total_amount
from {{ ref('source_orders') }}Make sure the unique key truly identifies each row uniquely to avoid unexpected duplicates.
If your unique key is not unique, dbt may raise errors or produce incorrect merges.
For composite keys, list all columns in the order that matches your data logic.
The unique_key tells dbt how to match rows when merging data.
It can be a single column or multiple columns combined.
Using the correct unique key keeps your data accurate and avoids duplicates.