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.
Practice
What is the main purpose of the unique_key in dbt's merge behavior?
Solution
Step 1: Understand the role of unique_key in merge
The unique_key tells dbt which rows in the target table correspond to rows in the source table during a merge operation.Step 2: Identify the correct behavior of unique_key
It helps dbt decide whether to update existing rows or insert new rows based on matching keys.Final Answer:
To identify which rows to update or insert during a merge -> Option DQuick Check:
unique_key matches rows for merge = D [OK]
- Thinking unique_key sorts data
- Believing unique_key creates columns
- Confusing unique_key with deleting rows
Which of the following is the correct way to specify a composite unique_key in a dbt model's merge config?
merge:
unique_key: ?Solution
Step 1: Recall syntax for composite unique_key
dbt expects a list of column names for composite keys, written as ['col1', 'col2'].Step 2: Match syntax options
['id', 'date'] uses a list syntax with square brackets and quotes, which is correct.Final Answer:
['id', 'date'] -> Option AQuick Check:
Composite unique_key = list of columns [OK]
- Using a string with commas instead of a list
- Using curly braces which is invalid syntax
- Trying to add columns with + operator
Given the following dbt merge config:
merge:
unique_key: 'user_id'And source data:
| user_id | value |
|---------|-------|
| 1 | 100 |
| 2 | 200 |
| 1 | 300 |What will happen when merging into a target table with existing user_id 1 and 2?
Solution
Step 1: Understand unique_key behavior with duplicates in source
dbt uses unique_key to match rows. If source has duplicates, the last row usually overwrites previous ones during merge.Step 2: Analyze the source data effect
Two rows with user_id 1 exist; the last row with value 300 will update the target row with user_id 1.Final Answer:
The last row with user_id 1 will update the target, overwriting previous -> Option BQuick Check:
Duplicate keys in source overwrite last row = C [OK]
- Expecting error on duplicate keys in source
- Thinking all duplicates insert as new rows
- Assuming only first duplicate updates
Consider this dbt merge config snippet:
merge:
unique_key: ['id']But the source data has duplicate id values. What is the likely issue?
Solution
Step 1: Identify problem with non-unique keys in merge
Unique_key must uniquely identify rows. If duplicates exist, dbt cannot correctly match rows.Step 2: Understand merge behavior with duplicates
Duplicates cause merge to fail or produce wrong updates because dbt doesn't know which row to update.Final Answer:
Merge will fail or produce incorrect results due to non-unique keys -> Option AQuick Check:
Non-unique keys cause merge failure = A [OK]
- Assuming merge ignores duplicates silently
- Thinking merge creates new columns for duplicates
- Believing merge auto-removes duplicates
You want to merge data from a source table into a target table using dbt. The source has columns user_id, event_date, and event_type. You want to update existing rows only if both user_id and event_date match. Which unique_key should you use?
Solution
Step 1: Identify columns needed to uniquely match rows
Since updates depend on matching both user_id and event_date, both must be part of unique_key.Step 2: Choose correct unique_key syntax
unique_key: ['user_id', 'event_date'] correctly uses a list with both columns to uniquely identify rows for merge.Final Answer:
unique_key: ['user_id', 'event_date'] -> Option CQuick Check:
Composite key matches both columns = B [OK]
- Using only user_id when event_date is needed
- Using event_type which is not unique key
- Mixing wrong columns in unique_key
Ready to prove it?
Pass the test and this pattern moves unknown
