diff --git a/lib/relation-definition.js b/lib/relation-definition.js index 9279fc7e..490d27c2 100644 --- a/lib/relation-definition.js +++ b/lib/relation-definition.js @@ -467,6 +467,10 @@ HasMany.prototype.findById = function (fkId, cb) { filter.where[idName] = fkId; filter.where[fk] = modelInstance[pk]; + if (filter.where[fk] === undefined) { + // Foreign key is undefined + return process.nextTick(cb); + } this.definition.applyScope(modelInstance, filter); modelTo.findOne(filter, function (err, inst) { @@ -919,6 +923,11 @@ BelongsTo.prototype.related = function (refresh, params) { var query = {where: {}}; query.where[pk] = modelInstance[fk]; + if (query.where[pk] === undefined) { + // Foreign key is undefined + return process.nextTick(cb); + } + this.definition.applyScope(modelInstance, query); modelTo.findOne(query, function (err, inst) {