Skip to content

Commit

Permalink
Fix #8022: Foreign key constraint on incremental model results in Dat…
Browse files Browse the repository at this point in the history
…abase Error (#8768)

(cherry picked from commit 6461f5a)
  • Loading branch information
aranke authored and github-actions[bot] committed Oct 10, 2023
1 parent 6510a8b commit 9e296f4
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 2 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20231004-154558.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Foreign key constraint on incremental model results in Database Error
time: 2023-10-04T15:45:58.298548+01:00
custom:
Author: aranke
Issue: "8022"
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
create {% if temporary: -%}temporary{%- endif %} table
{{ relation.include(database=(not temporary), schema=(not temporary)) }}
{% set contract_config = config.get('contract') %}
{% if contract_config.enforced %}
{% if contract_config.enforced and (not temporary) %}
{{ get_assert_columns_equivalent(sql) }}
{{ get_table_columns_and_constraints() }}
{%- set sql = get_select_subquery(sql) %}
Expand Down
2 changes: 1 addition & 1 deletion plugins/postgres/dbt/include/postgres/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
unlogged
{%- endif %} table {{ relation }}
{% set contract_config = config.get('contract') %}
{% if contract_config.enforced %}
{% if contract_config.enforced and (not temporary) %}
{{ get_assert_columns_equivalent(sql) }}
{{ get_table_columns_and_constraints() }} ;
insert into {{ relation }} (
Expand Down
44 changes: 44 additions & 0 deletions tests/adapter/dbt/tests/adapter/constraints/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,47 @@
- name: column_name
data_type: text
"""

create_table_macro_sql = """
{% macro create_table_macro() %}
create table if not exists numbers (n int not null primary key)
{% endmacro %}
"""

incremental_foreign_key_schema_yml = """
version: 2
models:
- name: raw_numbers
config:
contract:
enforced: true
materialized: table
columns:
- name: n
data_type: integer
constraints:
- type: primary_key
- type: not_null
- name: stg_numbers
config:
contract:
enforced: true
materialized: incremental
on_schema_change: append_new_columns
unique_key: n
columns:
- name: n
data_type: integer
constraints:
- type: foreign_key
expression: {schema}.raw_numbers (n)
"""

incremental_foreign_key_model_raw_numbers_sql = """
select 1 as n
"""

incremental_foreign_key_model_stg_numbers_sql = """
select * from {{ ref('raw_numbers') }}
"""
32 changes: 32 additions & 0 deletions tests/adapter/dbt/tests/adapter/constraints/test_constraints.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
my_model_contract_sql_header_sql,
my_model_incremental_contract_sql_header_sql,
model_contract_header_schema_yml,
create_table_macro_sql,
incremental_foreign_key_schema_yml,
incremental_foreign_key_model_raw_numbers_sql,
incremental_foreign_key_model_stg_numbers_sql,
)


Expand Down Expand Up @@ -530,3 +534,31 @@ def expected_sql(self):

class TestConstraintQuotedColumn(BaseConstraintQuotedColumn):
pass


class TestIncrementalForeignKeyConstraint:
@pytest.fixture(scope="class")
def macros(self):
return {
"create_table.sql": create_table_macro_sql,
}

@pytest.fixture(scope="class")
def models(self):
return {
"schema.yml": incremental_foreign_key_schema_yml,
"raw_numbers.sql": incremental_foreign_key_model_raw_numbers_sql,
"stg_numbers.sql": incremental_foreign_key_model_stg_numbers_sql,
}

def test_incremental_foreign_key_constraint(self, project):
unformatted_constraint_schema_yml = read_file("models", "schema.yml")
write_file(
unformatted_constraint_schema_yml.format(schema=project.test_schema),
"models",
"schema.yml",
)

run_dbt(["run", "--select", "raw_numbers"])
run_dbt(["run", "--select", "stg_numbers"])
run_dbt(["run", "--select", "stg_numbers"])

0 comments on commit 9e296f4

Please sign in to comment.