expect_compound_columns_to_be_unique
dbt-expectations
·
Model,Seed,Source
·
Uniqueness
How it Works
The expect_compound_columns_to_be_unique test from the dbt-expectations package verifies that the combination of values across a specified set of columns is unique for every row. This is useful when no single column acts as a primary key but a composite key is expected to be unique.
An optional ignore_row_if argument allows you to exclude rows with null values in any or all of the specified columns from the uniqueness check.
Steps and Conditions
Column List: Define the columns that together form the compound key.
Optional Null Handling: Use
ignore_row_ifto handle rows with null values.Execution: All column combinations are checked for duplicates.
Outcome: Pass if no duplicate combinations exist; fail if duplicates are found.
Example Usage: Logistics
A logistics company tracks delivery attempts where the combination of shipment_id and attempt_number should be unique, but neither column alone is a unique identifier.
This prevents duplicate delivery attempt records from corrupting fulfilment tracking reports.

