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);
    });
This commit is contained in:
Miroslav Bajtoš 2015-03-16 12:36:54 +01:00
parent 902772f1af
commit 6f11c2d717
2 changed files with 10 additions and 1 deletions

View File

@ -393,7 +393,7 @@ DataAccessObject.updateOrCreate = DataAccessObject.upsert = function upsert(data
function done(err, data) { function done(err, data) {
var obj; var obj;
if (data && !(data instanceof Model)) { if (data && !(data instanceof Model)) {
inst._initProperties(data); inst._initProperties(data, { persisted: true });
obj = inst; obj = inst;
} else { } else {
obj = data; obj = data;

View File

@ -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() { describe('findOrCreate', function() {