Skip to content

Commit 1f4ff37

Browse files
fix issue with many-to-many through with custom column names not being mapped correctly
1 parent 144f80a commit 1f4ff37

File tree

2 files changed

+6
-4
lines changed

2 files changed

+6
-4
lines changed

lib/waterline/model/lib/associationMethods/add.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -290,11 +290,12 @@ Add.prototype.createManyToMany = function(collection, attribute, pk, key, cb) {
290290

291291
// Grab the associated collection's primaryKey
292292
var collectionAttributes = this.collection.waterline.schema[attribute.collection.toLowerCase()];
293-
var associationKey = collectionAttributes.attributes[attribute.on].via;
293+
var associationKeyAttr = collectionAttributes.attributes[attribute.on] || collectionAttributes.attributes[attribute.via];
294+
var associationKey = associationKeyAttr.via;
294295

295296
// If this is a throughTable, look into the meta data cache for what key to use
296297
if (collectionAttributes.throughTable) {
297-
var cacheKey = collectionAttributes.throughTable[attribute.on + '.' + key];
298+
var cacheKey = collectionAttributes.throughTable[attribute.on + '.' + key] || collectionAttributes.throughTable[attribute.via + '.' + key];
298299
if (!cacheKey) {
299300
return cb(new Error('Unable to find the proper cache key in the through table definition'));
300301
}

lib/waterline/model/lib/associationMethods/remove.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,12 @@ Remove.prototype.removeManyToMany = function(collection, attribute, pk, key, cb)
228228

229229
// Grab the associated collection's primaryKey
230230
var collectionAttributes = this.collection.waterline.schema[attribute.collection.toLowerCase()];
231-
var associationKey = collectionAttributes.attributes[attribute.on].via;
231+
var associationKeyAttr = collectionAttributes.attributes[attribute.on] || collectionAttributes.attributes[attribute.via];
232+
var associationKey = associationKeyAttr.via;
232233

233234
// If this is a throughTable, look into the meta data cache for what key to use
234235
if (collectionAttributes.throughTable) {
235-
var cacheKey = collectionAttributes.throughTable[attribute.on + '.' + key];
236+
var cacheKey = collectionAttributes.throughTable[attribute.on + '.' + key] || collectionAttributes.throughTable[attribute.via + '.' + key];
236237
if (!cacheKey) {
237238
return cb(new Error('Unable to find the proper cache key in the through table definition'));
238239
}

0 commit comments

Comments
 (0)