不要怂,就是干,撸起袖子干!

Commit e695b92c by Gabe Gorelick Committed by Sushant

fix(model): handle Symbol-only where clauses (#8435)

1 parent 2a3b3dba
......@@ -83,7 +83,7 @@ class Model {
deletedAtObject[deletedAtAttribute.field || deletedAtCol] = deletedAtDefaultValue;
if (_.isEmpty(options.where)) {
if (Utils.isWhereEmpty(options.where)) {
options.where = deletedAtObject;
} else {
options.where = { [Op.and]: [deletedAtObject, options.where] };
......
......@@ -849,7 +849,7 @@ class QueryInterface {
options = _.clone(options);
if (!_.isEmpty(where)) {
if (!Utils.isWhereEmpty(where)) {
wheres.push(where);
}
......
......@@ -574,7 +574,7 @@ exports.mapIsolationLevelStringToTedious = (isolationLevel, tedious) => {
* @private
*/
function getOperators(obj) {
return _.intersection(Object.getOwnPropertySymbols(obj), operatorsArray);
return _.intersection(Object.getOwnPropertySymbols(obj || {}), operatorsArray);
}
exports.getOperators = getOperators;
......@@ -599,3 +599,15 @@ function getComplexSize(obj) {
return Array.isArray(obj) ? obj.length : getComplexKeys(obj).length;
}
exports.getComplexSize = getComplexSize;
/**
* Returns true if a where clause is empty, even with Symbols
*
* @param {Object} obj
* @return {Boolean}
* @private
*/
function isWhereEmpty(obj) {
return _.isEmpty(obj) && getOperators(obj).length === 0;
}
exports.isWhereEmpty = isWhereEmpty;
......@@ -708,6 +708,29 @@ describe(Support.getTestDialectTeaser('Model'), () => {
expect(u.username).to.equal('A fancy name');
});
});
// https://github.com/sequelize/sequelize/issues/8406
it('should work if model is paranoid and only operator in where clause is a Symbol', function() {
const User = this.sequelize.define('User', { username: Sequelize.STRING }, { paranoid: true } );
return User.sync({ force: true})
.then(() => {
return User.create({ username: 'foo' });
})
.then(() => {
return User.findOne({
where: {
[Sequelize.Op.or]: [
{ username: 'bar' },
{ username: 'baz' }
]
}
});
})
.then(user => {
expect(user).to.not.be.ok;
});
});
});
describe('findOrInitialize', () => {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!