diff --git a/.changes/unreleased/Features-20240912-153932.yaml b/.changes/unreleased/Features-20240912-153932.yaml new file mode 100644 index 00000000..8ef2d27d --- /dev/null +++ b/.changes/unreleased/Features-20240912-153932.yaml @@ -0,0 +1,6 @@ +kind: Features +body: delete+insert requires incremental_predicates if there's no unique_key +time: 2024-09-12T15:39:32.654501+08:00 +custom: + Author: dlord + Issue: https://github.com/dbt-labs/dbt-core/issues/10655 diff --git a/dbt/include/global_project/macros/materializations/models/incremental/merge.sql b/dbt/include/global_project/macros/materializations/models/incremental/merge.sql index ca972c9f..68dcc8f5 100644 --- a/dbt/include/global_project/macros/materializations/models/incremental/merge.sql +++ b/dbt/include/global_project/macros/materializations/models/incremental/merge.sql @@ -89,6 +89,18 @@ {%- endif -%}; {% endif %} + {% else %} + {% if not incremental_predicates %} + {{ exceptions.raise_compiler_error("incremental_predicates is required when there is no unique_key") }} + {% endif %} + + delete from {{ target }} + where ( + {% for predicate in incremental_predicates %} + {{ predicate|replace('DBT_INTERNAL_DEST', target)|replace('DBT_INTERNAL_SOURCE', source) }} + {{ "and " if not loop.last}} + {% endfor %} + ); {% endif %} insert into {{ target }} ({{ dest_cols_csv }})