diff --git a/lib/scope.js b/lib/scope.js index bfeac8c8..123f3282 100644 --- a/lib/scope.js +++ b/lib/scope.js @@ -130,7 +130,13 @@ ScopeDefinition.prototype.related = function(receiver, scopeParams, condOrRefres return mergedWhere; }; if (queryRelated.where !== undefined) { - queryRelated.where = buildWhere(); + if (!queryRelated.where[IdKey]) { + // If it's null, don't bother calling smartMerge or model.find. + // Simply return an empty result to the client. + return cb(null, []); + } else { + queryRelated.where = buildWhere(); + } } else { queryRelated.where = {}; queryRelated.where[IdKey] = collectTargetIds(data, IdKey); diff --git a/test/relations.test.js b/test/relations.test.js index 7ddb9c16..3eb5db92 100644 --- a/test/relations.test.js +++ b/test/relations.test.js @@ -819,6 +819,15 @@ describe('relations', function() { done(); }); }); + it('returns empty result when filtering with wrong id key', function(done) { + var wrongWhereFilter = {where: {wrongIdKey: samplePatientId}}; + physician.patients(wrongWhereFilter, function(err, ch) { + if (err) return done(err); + should.exist(ch); + ch.should.have.lengthOf(0); + done(); + }); + }); it('returns patients where id in an array', function(done) { var idArr = []; var whereFilter;