Add conflict resolution for auto-generated FK constraint names#1042
Open
dereuromark wants to merge 5 commits intofix-auto-generate-fk-constraint-namefrom
Open
Add conflict resolution for auto-generated FK constraint names#1042dereuromark wants to merge 5 commits intofix-auto-generate-fk-constraint-namefrom
dereuromark wants to merge 5 commits intofix-auto-generate-fk-constraint-namefrom
Conversation
When auto-generating FK constraint names, check if the name already exists and append a counter suffix (_2, _3, etc.) if needed. This prevents duplicate constraint name errors when multiple FKs are created on the same columns with different references.
Limit auto-generated foreign key constraint names to 125 characters to ensure the final name (including potential _XX counter suffix) stays within 128 characters. This prevents identifier length errors on databases with strict limits (MySQL: 64, PostgreSQL: 63).
- MySQL: 61 chars (64 limit - 3 for _XX suffix) - PostgreSQL: 60 chars (63 limit - 3 for _XX suffix) - SQL Server: 125 chars (128 limit - 3 for _XX suffix) - SQLite: No limit needed
Each adapter now defines its database-specific identifier length limit as a class constant, making the code more self-documenting.
Member
Author
|
Should the common code by moved to Util class? Regarding the public static function generateUniqueConstraintName(
string $baseName,
array $existingNames,
int $maxLength
): string |
Member
Author
|
@markstory I would squash-merge this first into the open PR, then we can merge the whole thing squashed for easier up/down merges. |
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.
Summary
I feel like when we now auto generate them instead of the db, we should do this, as there could be conflict somewhere along the migrations.
_2,_3, etc.) if a conflict is detectedUPDATE:
Also now respects technical max length limit per constraint using truncate.
We could also add hashing instead of just truncate + counter suffix.
This builds on #1041 which added auto-generation of FK constraint names.
Example
Changes
All 4 adapters updated with
getUniqueForeignKeyName()method:MysqlAdapterSqliteAdapterPostgresAdapterSqlserverAdapter