Skip to content

Commit

Permalink
fix: remove options.model overwrite on bulkUpdate (#15231)
Browse files Browse the repository at this point in the history
* fix: remove options.model overwrite on bulkUpdate

* fix: fix typo in test

* fix: make test simpler

Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>

Co-authored-by: Rik Smale <13023439+WikiRik@users.noreply.github.com>
  • Loading branch information
BigsonLvrocha and WikiRik committed Nov 7, 2022
1 parent 33e137e commit c620469
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/dialects/abstract/query-interface.js
Expand Up @@ -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;
Expand Down
47 changes: 47 additions & 0 deletions test/unit/model/update.test.js
Expand Up @@ -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);
});
});
});

0 comments on commit c620469

Please sign in to comment.