Skip to content

Commit

Permalink
fix(upsert): set isNewRecord to false when upserting (#12447) (#12485)
Browse files Browse the repository at this point in the history
  • Loading branch information
knoid committed Jul 11, 2020
1 parent 45d9c22 commit 96f4166
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/dialects/mariadb/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class Query extends AbstractQuery {
return data.affectedRows;
}
if (this.isUpsertQuery()) {
return [null, data.affectedRows === 1];
return [result, data.affectedRows === 1];
}
if (this.isInsertQuery(data)) {
this.handleInsertQuery(data);
Expand Down
3 changes: 3 additions & 0 deletions lib/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -2469,6 +2469,9 @@ class Model {
}
const result = await this.queryInterface.upsert(this.getTableName(options), insertValues, updateValues, instance.where(), options);

const [record] = result;
record.isNewRecord = false;

if (options.hooks) {
await this.runHooks('afterUpsert', result, options);
return result;
Expand Down
6 changes: 6 additions & 0 deletions test/integration/instance.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,12 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
expect(user.isNewRecord).to.not.be.ok;
});

it('returns false for upserted objects', async function() {
// adding id here so MSSQL doesn't fail. It needs a primary key to upsert
const [user] = await this.User.upsert({ id: 2, username: 'user' });
expect(user.isNewRecord).to.not.be.ok;
});

it('returns false for objects found by find method', async function() {
await this.User.create({ username: 'user' });
const user = await this.User.create({ username: 'user' });
Expand Down
2 changes: 1 addition & 1 deletion test/unit/model/upsert.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {

beforeEach(function() {
this.query = sinon.stub(current, 'query').resolves();
this.stub = sinon.stub(current.getQueryInterface(), 'upsert').resolves([true, undefined]);
this.stub = sinon.stub(current.getQueryInterface(), 'upsert').resolves([this.User.build(), true]);
});

afterEach(function() {
Expand Down

0 comments on commit 96f4166

Please sign in to comment.