Skip self-referencing associations for unconstrained keys#1094
Merged
Conversation
A column following the `<table>_id` naming on its own table (such as an external `system_id` on a `systems` table) made bake generate a self-referencing association whose alias equals the table's own name. The generated belongsTo and hasMany/hasOne then collided, and applying them threw "Association alias `Systems` is already set", aborting the bake run. Self-references are now only generated when the column is actually constrained as a foreign key to the same table. Legitimate self-refs such as `parent_id` keep their existing dedicated aliases, while convention-only `<table>_id` columns without a real constraint are skipped across the belongsTo, hasOne and hasMany detection passes.
ADmad
approved these changes
Jun 15, 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.
Addresses #1092
Problem
When a column follows the
<table>_idnaming on its own table - for example an externalsystem_idon asystemstable that is not a real foreign key - bake derived the aliasSystemsfor both the generatedbelongsTo(from the column) and the self-referencinghasMany/hasOnedetection. Applying the second association threw and aborted the whole bake run:Per the discussion in #1092 (cc the maintainer thread), legitimate self-references conventionally use
parent_id, which already gets dedicatedParent*/Child*aliases. A plain<table>_idthat is not backed by a foreign key is almost always an external identifier, not a self-association.Fix
Skip generating a self-referencing association when the resolved alias equals the table's own alias and the column is not actually constrained as a foreign key to that table. The guard is applied consistently across all three detection passes:
findBelongsTo()- skip the column unlessfindTableReferencedBy()confirms a real constraint.findHasMany()- skip self-table matches (theparent_idself-ref keeps its existingChild*handling).findHasOne()- skip self-table matches for unique columns.Real, constrained self-references (a
node_idforeign key referencingnodes.id) still produce theirbelongsTo, so no valid association is lost.Relationship to #1093
This is the upstream prevention (do not generate the bogus association). #1093 is independent downstream hardening (never fatal on a duplicate alias from any other cause). They compose and do not conflict.