Skip to content

Commit

Permalink
render to and to_columns on column and model level fk constraints
Browse files Browse the repository at this point in the history
  • Loading branch information
MichelleArk committed Jul 5, 2024
1 parent 7e9731c commit 424bcda
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
14 changes: 10 additions & 4 deletions dbt/adapters/base/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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:
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"',
]
Expand All @@ -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",
Expand Down

0 comments on commit 424bcda

Please sign in to comment.