From 6f11c2d7172cdae58e066a5b43b0effec1a7d6bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Mon, 16 Mar 2015 12:36:54 +0100 Subject: [PATCH] DAO: Fix updateOrCreate to set persisted:true Before this commit, the following code would not work: Change.updateOrCreate({...}, function(err, ch) { // somewhere later, modify "ch" and save the changes ch.save(cb); }); --- lib/dao.js | 2 +- test/manipulation.test.js | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/dao.js b/lib/dao.js index 50a0c273..35a79244 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 875ccf18..16eddd97 100644 --- a/test/manipulation.test.js +++ b/test/manipulation.test.js @@ -567,6 +567,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() {