Ensure unique association aliases for self-referencing keys#1093
Closed
dereuromark wants to merge 1 commit into
Closed
Ensure unique association aliases for self-referencing keys#1093dereuromark wants to merge 1 commit into
dereuromark wants to merge 1 commit into
Conversation
A non-foreign-key column matching the table name (such as a system_id column on a systems table) produced the same alias for both the generated belongsTo and hasMany association. The previous deduplication only recomputed the identical conventional name, so applying the second association threw "Association alias `Systems` is already set" and aborted baking entirely. Append a numeric suffix until the alias is unique, so bake generates valid, editable association code instead of failing.
Contributor
|
I don't think we should fix/support schemas, which don't follow our Naming Conventions See #1092 (comment) for more details |
Member
Author
|
Closing in favor of #1094, which prevents the invalid self-referencing association from being generated in the first place (the upstream fix). The alias de-duplication in this PR only changes behavior when two colliding associations share the same foreign key - which in practice is exactly the self-referencing case #1094 now suppresses. For ordinary collisions (e.g. a belongsTo and a hasMany to the same target via different keys) the existing code already disambiguates, so the numeric-suffix logic here is effectively redundant once #1094 lands. |
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.
Fixes #1092
Problem
When a table has a column whose conventional model name equals the table's own name (for example a non-foreign-key
system_idcolumn on asystemstable),bakederives the aliasSystemsfor both the generatedbelongsTo(from the column) and thehasMany(self-referencing detection). The deduplication inensureAliasUniqueness()caught the collision but its fallbackcreateAssociationAlias()recomputed the same conventional name from the foreign key, so the alias stayedSystems.Applying the second association then threw and aborted the whole bake run:
This is a follow-up to #1059 (which fixed the
parent_id-style two-key case).Fix
Append a numeric suffix until the alias is actually unique. The first occurrence keeps the conventional name; subsequent collisions become
Systems2,Systems3, and so on, while the original target class name is preserved via the existingclassNamehandling. Bake now generates valid, editable association code instead of crashing, leaving it to the developer to adjust or remove the guessed association.Notes
The signatures of
applyAssociations()andcreateAssociationAlias()are intentionally left unchanged to avoid breaking subclasses that override them.