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

Commit 07092263 by Jozef Hartinger Committed by Jan Aagaard Meier

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

1 parent e8d57201
Showing with 50 additions and 3 deletions
......@@ -1737,10 +1737,13 @@ class Model {
options = Utils.cloneDeep(options);
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
if (!options.where || !(Utils.isPrimitive(pkVal) || Buffer.isBuffer(pkVal))) {
// Don't add limit if querying directly on the pk or a unique column
if (!options.where || !_.some(options.where, (value, key) =>
(key === this.primaryKeyAttribute || _.includes(uniqueSingleColumns, key)) &&
(Utils.isPrimitive(value) || Buffer.isBuffer(value))
)) {
options.limit = 1;
}
}
......
......@@ -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!