Skip to content

fix: PK/FK constraints with an expression (e.g. RELY) dropped and re-added on every incremental run - #1552

Merged
sd-db merged 1 commit into
databricks:mainfrom
sd-db:sd-db/triage/dbt-1513-rely-constraint-churn
Jun 22, 2026
Merged

fix: PK/FK constraints with an expression (e.g. RELY) dropped and re-added on every incremental run#1552
sd-db merged 1 commit into
databricks:mainfrom
sd-db:sd-db/triage/dbt-1513-rely-constraint-churn

Conversation

@sd-db

@sd-db sd-db commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator

Description

Resolves #1513.

A contract-enforced incremental PK/FK declaring an expression was dropped and re-added on every run. The catalog read-back (information_schema) never carries the expression — no RELY/NORELY, and expression-form FKs come back as to/to_columns — so it never matched the model, and get_diff flagged the constraint as both add and drop every run (for any expression, not just RELY). The PK drop is DROP CONSTRAINT ... CASCADE, which silently drops dependent FKs and can INTERNAL_ERROR on newer UC.

Fix

Compare PK/FK on (type, name, columns) (dropping the un-round-trippable expression, and to/to_columns for FKs), but emit the original constraints so an ADD keeps its RELY. Explicit FK target changes (to/to_columns) are still reconciled.

Behavior change (accepted regression)

Changing the expression on an existing PK/FK — RELYNORELY, or an expression-form FK's target — is no longer applied on incremental runs; use --full-refresh. It only worked before as a side effect of the every-run recreate, and the catalog can't report the current state without fragile SHOW CREATE TABLE parsing. PK/FK are informational (NOT_ENFORCED), so this is metadata-only.

Tests

  • Unit (TestConstraintsConfig): RELY PK no-op, expression-form FK no-op, explicit FK repoint reconciled, new RELY PK keeps its expression.
  • Functional (TestIncrementalRelyConstraintReconciliation): a dependent FK survives an incremental re-run of a RELY-PK parent (asserted via information_schema); fails on main, passes here.

Checklist

  • Run in development; resolves the issue.
  • Includes tests.
  • CHANGELOG.md updated.

@sd-db
sd-db requested a review from jprakash-db as a code owner June 20, 2026 08:51
@sd-db
sd-db force-pushed the sd-db/triage/dbt-1513-rely-constraint-churn branch from b8b9bea to 18817e1 Compare June 20, 2026 08:52
@sd-db

sd-db commented Jun 20, 2026

Copy link
Copy Markdown
Collaborator Author

/integration-test

@github-actions

Copy link
Copy Markdown

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  dbt/adapters/databricks/relation_configs
  constraints.py 73
Project Total  

This report was generated by python-coverage-comment-action

@github-actions

Copy link
Copy Markdown

Integration tests dispatched for PR #1552 by @sd-db. Track progress in the Actions tab.

@github-actions

Copy link
Copy Markdown

Integration results for PR #1552 — UC cluster ✅ success · SQL warehouse ✅ success · All-purpose cluster ✅ success · Shard coverage ✅ success

Run details.

…LY expression

A primary/foreign key declared with an `expression` such as RELY was dropped
(DROP CONSTRAINT ... CASCADE) and re-added on every incremental run: RELY/NORELY
is not exposed by information_schema, so the catalog-read constraint never matched
the model's. The CASCADE silently dropped dependent foreign keys and could fail
with INTERNAL_ERROR on newer Unity Catalog.

Constraint reconciliation now compares PK/FK on (name, columns), ignoring the
un-round-trippable expression (and the expression-form FK's to/to_columns) while
still detecting genuine FK target changes.

Closes databricks#1513
@sd-db
sd-db force-pushed the sd-db/triage/dbt-1513-rely-constraint-churn branch from 6ffff20 to 999704d Compare June 22, 2026 04:59
@sd-db sd-db changed the title fix: stop spurious PK/FK constraint drops on incremental runs when expression (RELY) is set fix: PK/FK constraints with an expression (e.g. RELY) dropped and re-added on every incremental run Jun 22, 2026
@sd-db
sd-db merged commit 9ba6f8e into databricks:main Jun 22, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: Mismatch in PK & FK expression (e.g. RELY) triggers unnecessary constraint drops that crashes on incremental runs

2 participants