From 424bcdacb765b04eef172ca95d26309f9acaaeb3 Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Fri, 5 Jul 2024 14:44:02 -0400 Subject: [PATCH] render to and to_columns on column and model level fk constraints --- dbt/adapters/base/impl.py | 14 ++++++++++---- pyproject.toml | 4 ++-- 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/dbt/adapters/base/impl.py b/dbt/adapters/base/impl.py index ec590137..9ee8e59c 100644 --- a/dbt/adapters/base/impl.py +++ b/dbt/adapters/base/impl.py @@ -1602,8 +1602,11 @@ def render_column_constraint(cls, constraint: ColumnLevelConstraint) -> Optional rendered_column_constraint = f"unique {constraint_expression}" elif constraint.type == ConstraintType.primary_key: rendered_column_constraint = f"primary key {constraint_expression}" - elif constraint.type == ConstraintType.foreign_key and constraint_expression: - rendered_column_constraint = f"references {constraint_expression}" + elif constraint.type == ConstraintType.foreign_key: + if constraint.constraint_expression: + rendered_column_constraint = f"references {constraint_expression}" + elif constraint.to and constraint.to_columns: + rendered_column_constraint = f"references {constraint.to}({','.join(constraint.to_columns)})" elif constraint.type == ConstraintType.custom and constraint_expression: rendered_column_constraint = constraint_expression @@ -1690,8 +1693,11 @@ def render_model_constraint(cls, constraint: ModelLevelConstraint) -> Optional[s elif constraint.type == ConstraintType.primary_key: constraint_expression = f" {constraint.expression}" if constraint.expression else "" return f"{constraint_prefix}primary key{constraint_expression} ({column_list})" - elif constraint.type == ConstraintType.foreign_key and constraint.expression: - return f"{constraint_prefix}foreign key ({column_list}) references {constraint.expression}" + elif constraint.type == ConstraintType.foreign_key: + if constraint.constraint_expression: + return f"{constraint_prefix}foreign key ({column_list}) references {constraint.expression}" + elif constraint.to and constraint.to_columns: + return f"{constraint_prefix}foreign key ({column_list}) references {constraint.to}({','.join(constraint.to_columns)})" elif constraint.type == ConstraintType.custom and constraint.expression: return f"{constraint_prefix}{constraint.expression}" else: diff --git a/pyproject.toml b/pyproject.toml index e50aa63a..488bd659 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -57,7 +57,7 @@ include = ["dbt/adapters", "dbt/include", "dbt/__init__.py"] [tool.hatch.envs.default] dependencies = [ - "dbt_common @ git+https://github.com/dbt-labs/dbt-common.git", + "dbt_common @ git+https://github.com/dbt-labs/dbt-common.git@foreign-ref-column-constraint", 'pre-commit==3.7.0;python_version>="3.9"', 'pre-commit==3.5.0;python_version=="3.8"', ] @@ -67,7 +67,7 @@ code-quality = "pre-commit run --all-files" [tool.hatch.envs.unit-tests] dependencies = [ - "dbt_common @ git+https://github.com/dbt-labs/dbt-common.git", + "dbt_common @ git+https://github.com/dbt-labs/dbt-common.git@foreign-ref-column-constraint", "pytest", "pytest-dotenv", "pytest-xdist",