fix(belongsToMany): sync re-inserts rows on a column-mapped pivot#49
Merged
Conversation
selectPivotRows goes through the query adapter, which maps result rows back to attribute names via reverseColumnMap. So a pivot column like `permission_id` comes back under `permissionId` whenever the pivot table has a registered column map. sync read the related value with the raw column name (`row[this.relatedPivotKey]`), which was then undefined, so existingKeys stayed empty, every desired row looked new, and sync re-inserted rows that already existed, tripping the pivot's unique constraint. Read pivot columns under whichever key the row carries: raw column first (lightweight table loader), then the attribute alias (query adapter). Applied at the three sites that indexed a selected pivot row by the raw key: sync, attachPivotToResults, and getQuery, so eager loads and attribute attachment are fixed alongside sync. The existing pivot tests never used a column-mapped pivot table (role_users uses camelCase columns == attributes), which is why this escaped. A regression test needs a pivot whose db columns differ from its attribute names.
Codecov Report❌ Patch coverage is
📢 Thoughts on this report? Let us know! |
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.
selectPivotRows goes through the query adapter, which maps result rows back to attribute names via reverseColumnMap. So a pivot column like
permission_idcomes back underpermissionIdwhenever the pivot table has a registered column map. sync read the related value with the raw column name (row[this.relatedPivotKey]), which was then undefined, so existingKeys stayed empty, every desired row looked new, and sync re-inserted rows that already existed, tripping the pivot's unique constraint.Read pivot columns under whichever key the row carries: raw column first (lightweight table loader), then the attribute alias (query adapter). Applied at the three sites that indexed a selected pivot row by the raw key: sync, attachPivotToResults, and getQuery, so eager loads and attribute attachment are fixed alongside sync.
The existing pivot tests never used a column-mapped pivot table (role_users uses camelCase columns == attributes), which is why this escaped. A regression test needs a pivot whose db columns differ from its attribute names.