diff --git a/lib/connectors/memory.js b/lib/connectors/memory.js index 253ad19f..22002c67 100644 --- a/lib/connectors/memory.js +++ b/lib/connectors/memory.js @@ -210,6 +210,13 @@ Memory.prototype.create = function create(model, data, callback) { if(!this.collection(model)) { this.collection(model, {}); } + + if (this.collection(model)[id]) { + return process.nextTick(function() { + callback(new Error('Duplicate entry for ' + model + '.' + idName)); + }); + } + this.collection(model)[id] = serialize(data); this.saveToFile(id, callback); }; diff --git a/lib/dao.js b/lib/dao.js index 1177ecf0..3b68a68c 100644 --- a/lib/dao.js +++ b/lib/dao.js @@ -393,7 +393,7 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data function done(err, data) { var obj; if (data && !(data instanceof Model)) { - inst._initProperties(data); + inst._initProperties(data, { persisted: true }); obj = inst; } else { obj = data; diff --git a/test/manipulation.test.js b/test/manipulation.test.js index 1a16ffa4..e0df7896 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -291,6 +291,25 @@ describe('manipulation', function () { }); }); + it('should refuse to create object with duplicate id', function(done) { + // NOTE(bajtos) We cannot reuse Person model here, + // `settings.forceId` aborts the CREATE request at the validation step. + var Product = db.define('Product', { name: String }); + db.automigrate(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); + done(); + }); + }); + }); + }); }); describe('save', function () { @@ -574,6 +593,15 @@ describe('manipulation', function () { }); }); }); + + it('should allow save() of the created instance', function(done) { + Person.updateOrCreate( + { id: 'new-id', name: 'a-name' }, + function(err, inst) { + if (err) return done(err); + inst.save(done); + }); + }); }); describe('findOrCreate', function() {