duplicate value on unique index error (#4560)
This commit is contained in:
@@ -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();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -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.
|
||||
|
||||
Reference in New Issue
Block a user