Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix #8022: Foreign key constraint on incremental model results in Database Error #8768

Merged
merged 4 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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"])
Loading