Skip to content

Compare foreign key constraints by name - #7486

Merged
morozov merged 1 commit into
doctrine:5.0.xfrom
morozov:foreign-key-name-comparison
Aug 1, 2026
Merged

Compare foreign key constraints by name#7486
morozov merged 1 commit into
doctrine:5.0.xfrom
morozov:foreign-key-name-comparison

Conversation

@morozov

@morozov morozov commented Aug 1, 2026

Copy link
Copy Markdown
Member

Fixes #7410

Currently, the schema comparator ignores the name of a foreign key constraint: two constraints on the same columns are equal, whatever they're called.

If the application defines a schema with an FK named old_fk, deploys it to the database, then renames it to new_fk and performs a migration, the FK in the database will remain named old_fk without a diff reported.

This way, the resulting schema in the database is defined not only by the application-defined schema, but also by the path taken to reach it. This behavior is quite counterintuitive and isn't typical for DBAL.

#6413 attempted to make this change before and was reverted in #6445 due to the issue reported in #6437. The fact that an upgrade to a patch release (3.8.5) started reporting a schema mismatch was considered a breaking change. The change itself was right, but it was made in the wrong release.

ORM Behavior Considerations

From #6437, it looks like the ORM generates the FK name based on the columns it covers, but its users don't expect a rename when the underlying columns change. This expectation directly contradicts the "deployed schema is a function of the application-defined schema" idea.

If the ORM wants to retain this behavior (which is quite questionable), it needs to maintain a stable FK identity (once created and named, it stays named that way forever). However, even a migration to this behavior may itself result in a schema change, which is exactly the issue it would attempt to solve.

The most natural way to handle this is to accept schema changes.

Consistency with #7477

#7477 makes the comparator diff unique constraints. That's new functionality, so it gets the behavior right from the start and compares them by name. Neither leaving the current FK name comparison logic as is, nor mimicking it in the new functionality looks right to me.

Two foreign keys that differ only by name are two constraints, so the
comparator reports one of them as dropped and the other as added.
@morozov morozov added this to the 5.0.0 milestone Aug 1, 2026
@morozov
morozov marked this pull request as ready for review August 1, 2026 16:38
@morozov
morozov requested review from derrabus and greg0ire August 1, 2026 16:38
@morozov morozov linked an issue Aug 1, 2026 that may be closed by this pull request
@morozov
morozov merged commit 4b7a9ae into doctrine:5.0.x Aug 1, 2026
151 of 152 checks passed
@morozov
morozov deleted the foreign-key-name-comparison branch August 1, 2026 17:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Schema comparator does not detect renamed foreign keys

2 participants