Skip to content

Commit

Permalink
Add better error message re balderdashy/sails#4591
Browse files Browse the repository at this point in the history
  • Loading branch information
mikermcneil committed Feb 18, 2019
1 parent 9cbbb19 commit 9896677
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions lib/waterline/methods/add-to-collection.js
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,32 @@ module.exports = function addToCollection(/* targetRecordIds, collectionAttrName
});
}

// FUTURE: If anonymous junction model's primary key attribute is explicitly
// required, then this isn't going to work, because we're specifying
// a value for the primary key for the new junction records we're creating.
// We could, in waterline-schema (or possibly in sails-hook-orm or maybe
// even in Waterline core?), automatically un-require-ify the primary key
// attribute for anonymous junction models.
// > See https://github.com/balderdashy/sails/issues/4591 for background.
//
// But for now we just do this:
if (WLChild.junctionTable || WLChild.throughTable) {
if (WLChild.schema.id) {
if (WLChild.schema.id.required) {
throw new Error(
'Cannot add to the collection for this many-to-many association because the anonymous '+
'junction model\'s "id" (primary key) is required. This might mean that the default id '+
'in this app\'s `config/models.js` file makes all primary keys required. For more info, '+
'see https://github.com/balderdashy/sails/issues/4591. If you are unsure, check out '+
'https://sailsjs.com/support for help.'
);
}
} else {
// FUTURE: Maybe be smarter about this instead of just checking for `id`
// For now, we just ignore it and let the error happen.
}
}//fi


// ╔╗ ╦ ╦╦╦ ╔╦╗ ┌─┐ ┬ ┬┌─┐┬─┐┬ ┬
// ╠╩╗║ ║║║ ║║ │─┼┐│ │├┤ ├┬┘└┬┘
Expand Down

0 comments on commit 9896677

Please sign in to comment.