Skip to content

Commit 5cdcade

Browse files
committed
Fixes bug when querying equalTo on objectId and relation
- Adds $eq operator in transform - Makes $eq operator on objectId when adding $in operator
1 parent 53e7337 commit 5cdcade

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

spec/ParseRelation.spec.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,46 @@ describe('Parse.Relation testing', () => {
329329
});
330330
});
331331

332+
it("query on pointer and relation fields with equal bis", (done) => {
333+
var ChildObject = Parse.Object.extend("ChildObject");
334+
var childObjects = [];
335+
for (var i = 0; i < 10; i++) {
336+
childObjects.push(new ChildObject({x: i}));
337+
}
338+
339+
Parse.Object.saveAll(childObjects).then(() => {
340+
var ParentObject = Parse.Object.extend("ParentObject");
341+
var parent = new ParentObject();
342+
parent.set("x", 4);
343+
var relation = parent.relation("toChilds");
344+
relation.add(childObjects[0]);
345+
relation.add(childObjects[1]);
346+
relation.add(childObjects[2]);
347+
348+
var parent2 = new ParentObject();
349+
parent2.set("x", 3);
350+
parent2.relation("toChilds").add(childObjects[2]);
351+
352+
var parents = [];
353+
parents.push(parent);
354+
parents.push(parent2);
355+
parents.push(new ParentObject());
356+
357+
return Parse.Object.saveAll(parents).then(() => {
358+
var query = new Parse.Query(ParentObject);
359+
query.equalTo("objectId", parent2.id);
360+
// childObjects[2] is in 2 relations
361+
// before the fix, that woul yield 2 results
362+
query.equalTo("toChilds", childObjects[2]);
363+
364+
return query.find().then((list) => {
365+
equal(list.length, 1, "There should be 1 result");
366+
done();
367+
});
368+
});
369+
});
370+
});
371+
332372
it("or queries on pointer and relation fields", (done) => {
333373
var ChildObject = Parse.Object.extend("ChildObject");
334374
var childObjects = [];

src/Controllers/DatabaseController.js

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -455,13 +455,18 @@ DatabaseController.prototype.reduceRelationKeys = function(className, query) {
455455

456456
DatabaseController.prototype.addInObjectIdsIds = function(ids, query) {
457457
if (typeof query.objectId == 'string') {
458-
query.objectId = {'$in': [query.objectId]};
458+
// Add equality op as we are sure
459+
// we had a constraint on that one
460+
query.objectId = {'$eq': query.objectId};
459461
}
460-
query.objectId = query.objectId || {};
461-
let queryIn = [].concat(query.objectId['$in'] || [], ids || []);
462-
// make a set and spread to remove duplicates
463-
query.objectId = {'$in': [...new Set(queryIn)]};
464-
return query;
462+
query.objectId = query.objectId || {};
463+
let queryIn = [].concat(query.objectId['$in'] || [], ids || []);
464+
// make a set and spread to remove duplicates
465+
// replace the $in operator as other constraints
466+
// may be set
467+
query.objectId['$in'] = [...new Set(queryIn)];
468+
469+
return query;
465470
}
466471

467472
// Runs a query on the database.

src/transform.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,7 @@ function transformConstraint(constraint, inArray) {
412412
case '$gte':
413413
case '$exists':
414414
case '$ne':
415+
case '$eq':
415416
answer[key] = transformAtom(constraint[key], true,
416417
{inArray: inArray});
417418
break;

0 commit comments

Comments
 (0)