不要怂,就是干,撸起袖子干!

Commit 96f4166a by Ariel Barabas Committed by GitHub

fix(upsert): set isNewRecord to false when upserting (#12447) (#12485)

1 parent 45d9c226
...@@ -96,7 +96,7 @@ class Query extends AbstractQuery { ...@@ -96,7 +96,7 @@ class Query extends AbstractQuery {
return data.affectedRows; return data.affectedRows;
} }
if (this.isUpsertQuery()) { if (this.isUpsertQuery()) {
return [null, data.affectedRows === 1]; return [result, data.affectedRows === 1];
} }
if (this.isInsertQuery(data)) { if (this.isInsertQuery(data)) {
this.handleInsertQuery(data); this.handleInsertQuery(data);
......
...@@ -2469,6 +2469,9 @@ class Model { ...@@ -2469,6 +2469,9 @@ class Model {
} }
const result = await this.queryInterface.upsert(this.getTableName(options), insertValues, updateValues, instance.where(), options); const result = await this.queryInterface.upsert(this.getTableName(options), insertValues, updateValues, instance.where(), options);
const [record] = result;
record.isNewRecord = false;
if (options.hooks) { if (options.hooks) {
await this.runHooks('afterUpsert', result, options); await this.runHooks('afterUpsert', result, options);
return result; return result;
......
...@@ -85,6 +85,12 @@ describe(Support.getTestDialectTeaser('Instance'), () => { ...@@ -85,6 +85,12 @@ describe(Support.getTestDialectTeaser('Instance'), () => {
expect(user.isNewRecord).to.not.be.ok; 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() { it('returns false for objects found by find method', async function() {
await this.User.create({ username: 'user' }); await this.User.create({ username: 'user' });
const user = await this.User.create({ username: 'user' }); const user = await this.User.create({ username: 'user' });
......
...@@ -43,7 +43,7 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -43,7 +43,7 @@ describe(Support.getTestDialectTeaser('Model'), () => {
beforeEach(function() { beforeEach(function() {
this.query = sinon.stub(current, 'query').resolves(); 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() { afterEach(function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!