duplicate value on unique index error (#4560)

This commit is contained in:
Diamond Lewis
2018-02-09 07:39:35 -06:00
committed by GitHub
parent c16e6bb5e0
commit 3cd77eeb7c
2 changed files with 31 additions and 1 deletions

View File

@@ -2387,4 +2387,28 @@ describe('schemas', () => {
});
});
});
it_exclude_dbs(['postgres'])('cannot update to duplicate value on unique index', (done) => {
const index = {
code: 1
};
const obj1 = new Parse.Object('UniqueIndexClass');
obj1.set('code', 1);
const obj2 = new Parse.Object('UniqueIndexClass');
obj2.set('code', 2);
const adapter = config.database.adapter;
adapter._adaptiveCollection('UniqueIndexClass').then(collection => {
return collection._ensureSparseUniqueIndexInBackground(index);
}).then(() => {
return obj1.save();
}).then(() => {
return obj2.save();
}).then(() => {
obj1.set('code', 2);
return obj1.save();
}).then(done.fail).catch((error) => {
expect(error.code).toEqual(Parse.Error.DUPLICATE_VALUE);
done();
});
});
});

View File

@@ -421,7 +421,13 @@ export class MongoStorageAdapter implements StorageAdapter {
const mongoWhere = transformWhere(className, query, schema);
return this._adaptiveCollection(className)
.then(collection => collection._mongoCollection.findAndModify(mongoWhere, [], mongoUpdate, { new: true }))
.then(result => mongoObjectToParseObject(className, result.value, schema));
.then(result => mongoObjectToParseObject(className, result.value, schema))
.catch(error => {
if (error.code === 11000) {
throw new Parse.Error(Parse.Error.DUPLICATE_VALUE, 'A duplicate value for a field with unique values was provided');
}
throw error;
});
}
// Hopefully we can get rid of this. It's only used for config and hooks.