Skip to content

Commit

Permalink
Fix MSSQL returning values for bulkUpdate
Browse files Browse the repository at this point in the history
- bulkUpdate will now return the updates if options.returning is true.
- updateQuery will now take options.returning into account
- updateQuery removed duplicate setting check
- Fix documentation for model.update
  • Loading branch information
ShaharHD committed Jun 23, 2020
1 parent 7fba668 commit 94e0ecf
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 5 deletions.
4 changes: 2 additions & 2 deletions lib/dialects/abstract/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ class QueryGenerator {
}

if (this._dialect.supports.returnValues) {
if (this._dialect.supports.returnValues.output) {
if (this._dialect.supports.returnValues.output && options.returning) {
// we always need this for mssql
outputFragment = ' OUTPUT INSERTED.*';

Expand Down Expand Up @@ -393,7 +393,7 @@ class QueryGenerator {

suffix += selectFromTmp;
}
} else if (this._dialect.supports.returnValues && options.returning) {
} else if (options.returning) {
// ensure that the return output is properly mapped to model fields.
options.mapToModel = true;
suffix += ' RETURNING *';
Expand Down
5 changes: 4 additions & 1 deletion lib/dialects/mssql/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,10 @@ class Query extends AbstractQuery {
return data[0];
}
if (this.isBulkUpdateQuery()) {
return data.length;
if (this.options.returning)
return this.handleSelectQuery(data);
else
return data.length;
}
if (this.isBulkDeleteQuery()) {
return data[0] && data[0].AFFECTEDROWS;
Expand Down
2 changes: 1 addition & 1 deletion test/unit/sql/update.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ describe(Support.getTestDialectTeaser('SQL'), () => {

expectsql(sql.updateQuery(User.tableName, { username: 'new.username' }, { username: 'username' }, { limit: 1 }), {
query: {
mssql: 'UPDATE TOP(1) [Users] SET [username]=$1 OUTPUT INSERTED.* WHERE [username] = $2',
mssql: 'UPDATE TOP(1) [Users] SET [username]=$1 WHERE [username] = $2',
mariadb: 'UPDATE `Users` SET `username`=$1 WHERE `username` = $2 LIMIT 1',
mysql: 'UPDATE `Users` SET `username`=$1 WHERE `username` = $2 LIMIT 1',
sqlite: 'UPDATE `Users` SET `username`=$1 WHERE rowid IN (SELECT rowid FROM `Users` WHERE `username` = $2 LIMIT 1)',
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

0 comments on commit 94e0ecf

Please sign in to comment.