Skip to content

Ensure unique association aliases for self-referencing keys#1093

Closed
dereuromark wants to merge 1 commit into
3.xfrom
fix-1092-association-alias-uniqueness
Closed

Ensure unique association aliases for self-referencing keys#1093
dereuromark wants to merge 1 commit into
3.xfrom
fix-1092-association-alias-uniqueness

Conversation

@dereuromark

Copy link
Copy Markdown
Member

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_id column on a systems table), bake derives the alias Systems for both the generated belongsTo (from the column) and the hasMany (self-referencing detection). The deduplication in ensureAliasUniqueness() caught the collision but its fallback createAssociationAlias() recomputed the same conventional name from the foreign key, so the alias stayed Systems.

Applying the second association then threw and aborted the whole bake run:

[Cake\Core\Exception\CakeException] Association alias `Systems` is already set.

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 existing className handling. 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() and createAssociationAlias() are intentionally left unchanged to avoid breaking subclasses that override them.

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.
@LordSimal

Copy link
Copy Markdown
Contributor

I don't think we should fix/support schemas, which don't follow our Naming Conventions

See #1092 (comment) for more details

@dereuromark

Copy link
Copy Markdown
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.

@dereuromark dereuromark deleted the fix-1092-association-alias-uniqueness branch June 15, 2026 14:01
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.

Option to ignore Association alias 'Foobar' is already set errors while baking

2 participants