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

Commit 1139fa0b by Mick Hansen

should be possible use limit and a where on a hasMany with a through model with additional includes

1 parent 6497a489
......@@ -572,22 +572,44 @@ module.exports = (function() {
, tableSource = parentTable
, identSource = association.identifier
, attrSource = ((!association.source.hasPrimaryKeys || primaryKeysSource.length !== 1) ? 'id' : primaryKeysSource[0])
, where
var primaryKeysTarget = Object.keys(association.target.primaryKeys)
, primaryKeysTarget = Object.keys(association.target.primaryKeys)
, tableTarget = as
, identTarget = association.foreignIdentifier
, attrTarget = ((!include.association.target.hasPrimaryKeys || primaryKeysTarget.length !== 1) ? 'id' : primaryKeysTarget[0])
, sourceJoinOn
, targetJoinOn
, targetWhere
sourceJoinOn = self.quoteIdentifier(tableSource) + "." + self.quoteIdentifier(attrSource) + " = "
sourceJoinOn += self.quoteIdentifier(throughAs) + "." + self.quoteIdentifier(identSource)
targetJoinOn = self.quoteIdentifier(tableTarget) + "." + self.quoteIdentifier(attrTarget) + " = "
targetJoinOn += self.quoteIdentifier(throughAs) + "." + self.quoteIdentifier(identTarget)
joinQueryItem += joinType + self.quoteIdentifier(throughTable) + " AS " + self.quoteIdentifier(throughAs) + " ON "
joinQueryItem += self.quoteIdentifier(tableSource) + "." + self.quoteIdentifier(attrSource) + " = "
joinQueryItem += self.quoteIdentifier(throughAs) + "." + self.quoteIdentifier(identSource)
joinQueryItem += sourceJoinOn
joinQueryItem += joinType + self.quoteIdentifier(table) + " AS " + self.quoteIdentifier(as) + " ON "
joinQueryItem += self.quoteIdentifier(tableTarget) + "." + self.quoteIdentifier(attrTarget) + " = "
joinQueryItem += self.quoteIdentifier(throughAs) + "." + self.quoteIdentifier(identTarget)
joinQueryItem += targetJoinOn
if (include.where) {
joinQueryItem += " AND "+self.hashToWhereConditions(includeWhere, include.daoFactory, whereOptions)
targetWhere = self.hashToWhereConditions(includeWhere, include.daoFactory, whereOptions)
joinQueryItem += " AND "+ targetWhere
if (subQuery) {
if (!options.where) options.where = {}
var _where = "(SELECT "+self.quoteIdentifier(identSource)+" FROM " + self.quoteIdentifier(throughTable) + " AS " + self.quoteIdentifier(throughAs);
_where += joinType + self.quoteIdentifier(table) + " AS " + self.quoteIdentifier(as) + " ON ";
_where += targetJoinOn;
_where += " WHERE " + sourceJoinOn + " AND " + targetWhere + " LIMIT 1)";
_where += " IS NOT NULL"
options.where["__"+throughAs] = self.sequelize.asIs(_where)
}
}
} else {
var primaryKeysLeft = ((association.associationType === 'BelongsTo') ? Object.keys(association.target.primaryKeys) : Object.keys(include.association.source.primaryKeys))
......
......@@ -1144,7 +1144,7 @@ describe(Support.getTestDialectTeaser("Include"), function () {
})
})
it.only('should be possible use limit and a where on a hasMany with additional includes', function (done) {
it('should be possible use limit and a where on a hasMany with additional includes', function (done) {
var self = this
this.fixtureA(function () {
self.models.Product.findAll({
......@@ -1173,5 +1173,33 @@ describe(Support.getTestDialectTeaser("Include"), function () {
})
})
})
it('should be possible use limit and a where on a hasMany with a through model with additional includes', function (done) {
var self = this
this.fixtureA(function () {
self.models.Product.findAll({
include: [
{model: self.models.Company},
{model: self.models.Tag, where: {name: ['A', 'B','C']}},
{model: self.models.Price}
],
limit: 10,
order: 'id ASC'
}).done(function (err, products) {
expect(err).not.to.be.ok
expect(products.length).to.equal(10)
products.forEach(function (product) {
expect(product.tags.length).to.be.ok
expect(product.prices.length).to.be.ok
product.tags.forEach(function (tag) {
expect(['A', 'B', 'C']).to.include(tag.name)
})
})
done()
})
})
})
})
})
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!