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 (mssql): bulkUpdate returning values #12410

Merged
merged 5 commits into from
Jun 26, 2020
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
6 changes: 5 additions & 1 deletion lib/dialects/mssql/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,11 @@ class Query extends AbstractQuery {
return data[0];
}
if (this.isBulkUpdateQuery()) {
return data.length;
if (this.options.returning) {
return this.handleSelectQuery(data);
}

return rowCount;
}
if (this.isBulkDeleteQuery()) {
return data[0] && data[0].AFFECTEDROWS;
Expand Down
19 changes: 19 additions & 0 deletions test/integration/model/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,25 @@ describe(Support.getTestDialectTeaser('Model'), () => {
});
}

if (_.get(current.dialect.supports, 'returnValues.output')) {
it('should output the updated record', function() {
return this.Account.create({ ownerId: 2 }).then(account => {
return this.Account.update({ name: 'FooBar' }, {
where: {
id: account.get('id')
},
returning: true
}).then(([, accounts]) => {
const firstAcc = accounts[0];
expect(firstAcc.name).to.be.equal('FooBar');
return firstAcc.reload();
}).then(accountReloaded => {
expect(accountReloaded.ownerId, 'Reloaded as output update only return primary key and changed fields').to.be.equal(2);
});
});
});
}

if (current.dialect.supports['LIMIT ON UPDATE']) {
it('should only update one row', function() {
return this.Account.create({
Expand Down
2 changes: 1 addition & 1 deletion types/lib/model.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2043,7 +2043,7 @@ export abstract class Model<T = any, T2 = any> extends Hooks {
/**
* Update multiple instances that match the where options. The promise returns an array with one or two
* elements. The first element is always the number of affected rows, while the second element is the actual
* affected rows (only supported in postgres with `options.returning` true.)
* affected rows (only supported in postgres and mssql with `options.returning` true.)
*/
public static update<M extends Model>(
this: { new (): M } & typeof Model,
Expand Down