Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PATCH: Fix for when a through table has multiple associations #1618

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions lib/waterline/methods/add-to-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -349,11 +349,16 @@ module.exports = function addToCollection(/* targetRecordIds, collectionAttrName
// through table mapping that was generated by Waterline-Schema.
else if (_.has(Object.getPrototypeOf(WLChild), 'throughTable')) {
childReference = WLChild.throughTable[WLModel.identity + '.' + query.collectionAttrName];
_.each(WLChild.throughTable, function(rhs, key) {
if (key !== WLModel.identity + '.' + query.collectionAttrName) {
parentReference = rhs;
}
});

parentReference = WLModel.identity;

// this code assumed that there would be only 2 associations on the through table
// but in some projects there are multiple
// _.each(WLChild.throughTable, function(rhs, key) {
// if (key !== WLModel.identity + '.' + query.collectionAttrName) {
// parentReference = rhs;
// }
// });
}

// Find the child reference in a junction table
Expand Down
15 changes: 10 additions & 5 deletions lib/waterline/methods/replace-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -358,11 +358,16 @@ module.exports = function replaceCollection(/* targetRecordIds?, collectionAttrN
// through table mapping that was generated by Waterline-Schema.
else if (_.has(Object.getPrototypeOf(WLChild), 'throughTable')) {
childReference = WLChild.throughTable[WLModel.identity + '.' + query.collectionAttrName];
_.each(WLChild.throughTable, function(rhs, key) {
if (key !== WLModel.identity + '.' + query.collectionAttrName) {
parentReference = rhs;
}
});

// should be safe to assume that the parentReference is indeed the main model here
parentReference = WLModel.identity;

// this code assumed that there would be only 2 associations on the through table (as stated above)
// _.each(WLChild.throughTable, function(rhs, key) {
// if (key !== WLModel.identity + '.' + query.collectionAttrName) {
// parentReference = rhs;
// }
// });
}//>-


Expand Down