diff --git a/lib/connectors/memory.js b/lib/connectors/memory.js index 0e2c817a..ae8b260d 100644 --- a/lib/connectors/memory.js +++ b/lib/connectors/memory.js @@ -220,8 +220,11 @@ Memory.prototype._createSync = function(model, data, fn) { this.collection(model, {}); } - if (this.collection(model)[id]) - return fn(new Error(g.f('Duplicate entry for %s.%s', model, idName))); + if (this.collection(model)[id]) { + var error = new Error(g.f('Duplicate entry for %s.%s', model, idName)); + error.statusCode = error.status = 409; + return fn(error); + } this.collection(model)[id] = serialize(data); fn(null, id); diff --git a/test/memory.test.js b/test/memory.test.js index 3fd69bc5..f72ddefb 100644 --- a/test/memory.test.js +++ b/test/memory.test.js @@ -660,6 +660,26 @@ describe('Memory connector', function() { }); }); + it('should refuse to create object with duplicate id', function(done) { + var ds = new DataSource({connector: 'memory'}); + var Product = ds.define('ProductTest', {name: String}, {forceId: false}); + ds.automigrate('ProductTest', function(err) { + if (err) return done(err); + + Product.create({name: 'a-name'}, function(err, p) { + if (err) return done(err); + Product.create({id: p.id, name: 'duplicate'}, function(err) { + if (!err) { + return done(new Error('Create should have rejected duplicate id.')); + } + err.message.should.match(/duplicate/i); + err.statusCode.should.equal(409); + done(); + }); + }); + }); + }); + describe('automigrate', function() { var ds; beforeEach(function() {