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

Commit f0ae18d6 by Jozef Hartinger Committed by Jan Aagaard Meier

fix(model): don't add LIMIT in findOne() queries on unique key (#9273)

1 parent 3b623cbf
Showing with 50 additions and 3 deletions
...@@ -1745,10 +1745,13 @@ class Model { ...@@ -1745,10 +1745,13 @@ class Model {
options = Utils.cloneDeep(options); options = Utils.cloneDeep(options);
if (options.limit === undefined) { if (options.limit === undefined) {
const pkVal = options.where && options.where[this.primaryKeyAttribute]; const uniqueSingleColumns = _.chain(this.uniqueKeys).values().filter(c => c.fields.length === 1).map('column').value();
// Don't add limit if querying directly on the pk // Don't add limit if querying directly on the pk or a unique column
if (!options.where || !(Utils.isPrimitive(pkVal) || Buffer.isBuffer(pkVal))) { if (!options.where || !_.some(options.where, (value, key) =>
(key === this.primaryKeyAttribute || _.includes(uniqueSingleColumns, key)) &&
(Utils.isPrimitive(value) || Buffer.isBuffer(value))
)) {
options.limit = 1; options.limit = 1;
} }
} }
......
...@@ -67,5 +67,49 @@ describe(Support.getTestDialectTeaser('Model'), () => { ...@@ -67,5 +67,49 @@ describe(Support.getTestDialectTeaser('Model'), () => {
}); });
}); });
describe('should not add limit when querying on an unique key', () => {
it('with custom unique key', function() {
const Model = current.define('model', {
unique: {
type: DataTypes.INTEGER,
unique: true
}
});
return Model.findOne({ where: { unique: 42 }}).bind(this).then(function() {
expect(this.stub.getCall(0).args[0]).to.be.an('object').not.to.have.property('limit');
});
});
it('with blob unique key', function() {
const Model = current.define('model', {
unique: {
type: DataTypes.BLOB,
unique: true
}
});
return Model.findOne({ where: { unique: new Buffer('foo') }}).bind(this).then(function() {
expect(this.stub.getCall(0).args[0]).to.be.an('object').not.to.have.property('limit');
});
});
});
it('should add limit when using multi-column unique key', function() {
const Model = current.define('model', {
unique1: {
type: DataTypes.INTEGER,
unique: 'unique'
},
unique2: {
type: DataTypes.INTEGER,
unique: 'unique'
}
});
return Model.findOne({ where: { unique1: 42}}).bind(this).then(function() {
expect(this.stub.getCall(0).args[0]).to.be.an('object').to.have.property('limit');
});
});
}); });
}); });
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!