Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: remove options.model overwrite on bulkUpdate #15252

Merged
merged 2 commits into from Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dialects/abstract/query-interface.js
Expand Up @@ -923,7 +923,7 @@ class QueryInterface {

const sql = this.queryGenerator.updateQuery(tableName, values, identifier, options, attributes);
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 @@ -49,4 +49,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, 'query').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);
});
});
});