diff --git a/src/dialects/abstract/query-interface.js b/src/dialects/abstract/query-interface.js index d19dec0355a8..b19ecb920430 100644 --- a/src/dialects/abstract/query-interface.js +++ b/src/dialects/abstract/query-interface.js @@ -1002,7 +1002,7 @@ export class QueryInterface { const { bind, query } = this.queryGenerator.updateQuery(tableName, values, where, options, columnDefinitions); const table = _.isObject(tableName) ? tableName : { tableName }; - const model = _.find(this.sequelize.modelManager.models, { tableName: table.tableName }); + const model = options.model ? options.model : _.find(this.sequelize.modelManager.models, { tableName: table.tableName }); options.type = QueryTypes.BULKUPDATE; options.model = model; diff --git a/test/unit/model/update.test.js b/test/unit/model/update.test.js index bd943f810446..da63c4852c1d 100644 --- a/test/unit/model/update.test.js +++ b/test/unit/model/update.test.js @@ -53,4 +53,51 @@ describe(Support.getTestDialectTeaser('Model'), () => { await expect(this.User.update(this.updates, { where: new Where() })).to.be.rejected; }); }); + + describe('Update with multiple models to the same table', () => { + before(function () { + this.Model1 = current.define('Model1', { + value: DataTypes.INTEGER, + name: DataTypes.STRING, + isModel2: DataTypes.BOOLEAN, + model1ExclusiveData: DataTypes.STRING, + }, { + tableName: 'model_table', + }); + + this.Model2 = current.define('Model2', { + value: DataTypes.INTEGER, + name: DataTypes.STRING, + }, { + tableName: 'model_table', + }); + }); + + beforeEach(function () { + this.stubQuery = sinon.stub(current, 'queryRaw').resolves([]); + }); + + afterEach(function () { + this.stubQuery.restore(); + }); + + it('updates model1 using model1 model', async function () { + await this.Model1.update({ + name: 'other name', + model1ExclusiveData: 'only I can update this field', + }, { + where: { value: 1 }, + }); + expect(this.stubQuery.lastCall.lastArg.model).to.eq(this.Model1); + }); + + it('updates model2 using model2 model', async function () { + await this.Model2.update({ + name: 'other name', + }, { + where: { value: 2 }, + }); + expect(this.stubQuery.lastCall.lastArg.model).to.eq(this.Model2); + }); + }); });