fix: deterministically name unnamed PK/FK so incremental constraints don't churn or collide#1561
Open
sd-db wants to merge 3 commits into
Open
fix: deterministically name unnamed PK/FK so incremental constraints don't churn or collide#1561sd-db wants to merge 3 commits into
sd-db wants to merge 3 commits into
Conversation
…don't churn or collide Databricks auto-names an unnamed primary/foreign key, so the model's name=None never matched the catalog read and the incremental diff dropped and re-added it every run (silently dropping dependent FKs); multiple unnamed FKs to one parent collided on the server-derived <table>_<parent>_fk name. dbt now assigns an unnamed PK/FK the same deterministic name its create-time macro writes (synthesize_constraint_name), generated identically on the create and incremental paths, so the model and catalog agree and the diff is a no-op. A foreign key's name folds in its referenced columns, so distinct FKs never collide. Macro/Python name parity is pinned by unit tests. Closes #1333 Closes #1344 Supersedes #1554
Coverage reportClick to see where and how coverage changed
This report was generated by python-coverage-comment-action |
||||||||||||||||||||||||||||||||||||
Collaborator
Author
|
/integration-test |
|
Integration tests dispatched for PR #1561 by @sd-db. Track progress in the Actions tab. |
|
Integration results for PR #1561 — UC cluster ✅ success · SQL warehouse ❌ failure · All-purpose cluster ✅ success · Shard coverage ✅ success |
jprakash-db
approved these changes
Jul 2, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
Unnamed primary/foreign keys churned on every incremental run, and two or more unnamed foreign keys to the same parent failed with
DELTA_CONSTRAINT_ALREADY_EXISTS. Databricks auto-names an unnamed key, so the model'sname=Nonenever matched the catalog read; the diff issuedDROP CONSTRAINT ... CASCADE+ re-add each run (silently dropping dependent FKs), and the unnamed FK re-adds collided on the server-derived<table>_<parent>_fkname.Fix
dbt now assigns an unnamed PK/FK the same deterministic name its create-time macro already writes (
synthesize_constraint_name), generated identically on the create and incremental paths, so the model and catalog agree and the diff is a no-op. A foreign key's name folds in its referenced columns (its full identity), so distinct FKs never collide — and a config that currently fails at create (two unnamed FKs from one column to different parent columns) becomes possible.get_diff— once names are deterministic, the existing(name, columns)comparison + repoint reconciliation just work.TestConstraintNameParityso the two implementations can't drift while both exist.Behavior
Existing unnamed foreign keys are renamed once on the next incremental run (a no-op
DROP/re-add, no cascade), then stable — consistent with #1551 foldingexpressioninto the PK name in this same release. An unnamed key'sexpression/target edit is now reconciled (its name encodes it), unlike a named key (#1552 — still needs--full-refresh); the #1552 changelog note is scoped to named keys accordingly.Tests
from_relation_confignames unnamed PK/FK;get_diffno-ops for matched and two-FK-to-one-parent cases (with a negative assertion); macro↔Python parity for FK (single/multi/expression) and PK (incl. RELY); distinctto_columns→ distinct names.use_materialization_v2: false): two unnamed FKs to one parent survive an incremental run (1.11.5 fails with DELTA_CONSTRAINT_ALREADY_EXISTS in incremental models with two or more foreign keys to same relation #1344); an unnamedRELYPK's dependent FK survives an incremental re-run (TABLE_OR_VIEW_NOT_FOUND on constraint DROP during V2 incremental materialization #1333 + [BUG]: Mismatch in PK & FKexpression(e.g. RELY) triggers unnecessary constraint drops that crashes on incremental runs #1513). Both verified RED→GREEN on a live warehouse.Supersedes #1554 (its unnamed-PK fix is reimplemented here more generally; its RELY-PK regression test is carried forward as an unnamed-PK variant).
Closes #1333
Closes #1344