Creating an incremental model using the MERGE strategy with a unique key. The unique key to merge on is a surrogate key of columns from the table.
When I run this incremental model I get the error: Duplicate row detected during DML action.
I have confirmed this key is unique for incrementation so I am very confused by this error.
-- Model Configuration
{{
config(
materialized = 'incremental',
unique_key = ['loan_history_key']
)
}}
-- Import dependencies
-- depends_on: {{ ref('dim_loans') }}
-- depends_on: {{ ref('dim_cifs') }}
-- depends_on: {{ ref('fact_loan_details') }}
-- depends_on: {{ ref('fact_loan_payments') }}
-- depends_on: {{ ref('dim_dates') }}
-- Select data and generate surrogate key
WITH loan_histories AS (
SELECT
dim_loans.loan_key AS loan_key,
dim_dates.date_key AS as_of_date_key,
-- Add other necessary fields
FROM
{{ ref('dim_loans') }} AS dim_loans
JOIN
{{ ref('dim_dates') }} AS dim_dates
ON dim_loans.some_date = dim_dates.date
-- Include other necessary joins
)
SELECT
{{ dbt_utils.generate_surrogate_key(['loan_key', 'as_of_date_key']) }} AS loan_history_key,
*
FROM
loan_histories
-- Handle incremental updates
{% if is_incremental() %}
WHERE
TO_DATE(TO_CHAR(as_of_date_key), 'YYYYMMDD') >= current_date - interval '3 day'
{% endif %}
1