Skip to content

Commit

Permalink
fix(model.destroy): return 0 with truncate (#12281)
Browse files Browse the repository at this point in the history
  • Loading branch information
JuarezLustosa committed Jun 8, 2020
1 parent 72925cf commit ed2d7a9
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/dialects/mssql/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ class Query extends AbstractQuery {
return rowCount;
}
if (this.isBulkDeleteQuery()) {
return data[0] && data[0].AFFECTEDROWS;
return data[0] ? data[0].AFFECTEDROWS : 0;
}
if (this.isVersionQuery()) {
return data[0].version;
Expand Down
3 changes: 1 addition & 2 deletions lib/dialects/postgres/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class Query extends AbstractQuery {
(count, r) => Number.isFinite(r.rowCount) ? count + r.rowCount : count,
0
)
: queryResult.rowCount;
: queryResult.rowCount || 0;

if (this.sequelize.options.minifyAliases && this.options.aliasesMapping) {
rows = rows
Expand Down Expand Up @@ -396,7 +396,6 @@ class Query extends AbstractQuery {
}
}


module.exports = Query;
module.exports.Query = Query;
module.exports.default = Query;
9 changes: 9 additions & 0 deletions test/integration/model.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,15 @@ describe(Support.getTestDialectTeaser('Model'), () => {
expect(await User.findAll()).to.have.lengthOf(0);
});

it('`truncate` option returns a number', async function() {
const User = this.sequelize.define('User', { username: DataTypes.STRING });
await this.sequelize.sync({ force: true });
await User.bulkCreate([{ username: 'user1' }, { username: 'user2' }]);
const affectedRows = await User.destroy({ truncate: true });
expect(await User.findAll()).to.have.lengthOf(0);
expect(affectedRows).to.be.a('number');
});

it('throws an error if no where clause is given', async function() {
const User = this.sequelize.define('User', { username: DataTypes.STRING });
await this.sequelize.sync({ force: true });
Expand Down

0 comments on commit ed2d7a9

Please sign in to comment.