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

Commit 8c100802 by Mick Hansen

should support a simple nested hasMany <-> hasMany include

1 parent af848776
......@@ -399,13 +399,18 @@ module.exports = (function() {
, attrSource = ((!include.association.source.hasPrimaryKeys || primaryKeysSource.length !== 1) ? 'id' : primaryKeysSource[0])
var primaryKeysTarget = Object.keys(include.association.target.primaryKeys)
, tableTarget = include.as
, tableTarget = as
, identTarget = include.association.foreignIdentifier
, attrTarget = ((!include.association.target.hasPrimaryKeys || primaryKeysTarget.length !== 1) ? 'id' : primaryKeysTarget[0])
var tableJunction = include.association.through.tableName
joinQueryItem += " LEFT OUTER JOIN " + self.quoteIdentifier(tableJunction) + " ON " + self.quoteIdentifier(tableSource) + "." + self.quoteIdentifier(attrSource) + " = " + self.quoteIdentifier(tableJunction) + "." + self.quoteIdentifier(identSource)
joinQueryItem += " LEFT OUTER JOIN " + self.quoteIdentifier(table) + " AS " + self.quoteIdentifier(as) + " ON " + self.quoteIdentifier(tableTarget) + "." + self.quoteIdentifier(attrTarget) + " = " + self.quoteIdentifier(tableJunction) + "." + self.quoteIdentifier(identTarget)
joinQueryItem += " LEFT OUTER JOIN " + self.quoteIdentifier(tableJunction) + " ON "
joinQueryItem += self.quoteIdentifier(tableSource) + "." + self.quoteIdentifier(attrSource) + " = "
joinQueryItem += self.quoteIdentifier(tableJunction) + "." + self.quoteIdentifier(identSource)
joinQueryItem += " LEFT OUTER JOIN " + self.quoteIdentifier(table) + " AS " + self.quoteIdentifier(as) + " ON "
joinQueryItem += self.quoteIdentifier(tableTarget) + "." + self.quoteIdentifier(attrTarget) + " = "
joinQueryItem += self.quoteIdentifier(tableJunction) + "." + self.quoteIdentifier(identTarget)
} else {
var primaryKeysLeft = ((include.association.associationType === 'BelongsTo') ? Object.keys(include.association.target.primaryKeys) : Object.keys(include.association.source.primaryKeys))
, tableLeft = ((include.association.associationType === 'BelongsTo') ? as : parentTable)
......
......@@ -220,7 +220,6 @@ module.exports = (function() {
// Queries with include
} else if (this.options.hasJoin === true) {
results = groupJoinData(results, this.options)
console.log(JSON.stringify(results, null, '\t'));
result = results.map(function(result) {
return this.callee.build(result, {
isNewRecord: false,
......
......@@ -221,5 +221,80 @@ describe(Support.getTestDialectTeaser("Include"), function () {
})
})
})
it('should support a simple nested hasMany <-> hasMany include', function (done) {
var User = this.sequelize.define('User', {})
, Product = this.sequelize.define('Product', {
title: DataTypes.STRING
})
, Tag = this.sequelize.define('Tag', {
name: DataTypes.STRING
})
User.hasMany(Product)
Product.hasMany(Tag)
Tag.hasMany(Product)
this.sequelize.sync({force: true}).done(function () {
async.auto({
user: function (callback) {
User.create().done(callback)
},
products: function (callback) {
Product.bulkCreate([
{title: 'Chair'},
{title: 'Desk'},
{title: 'Dress'},
{title: 'Bed'}
]).done(function () {
Product.findAll().done(callback)
})
},
tags: function(callback) {
Tag.bulkCreate([
{name: 'A'},
{name: 'B'},
{name: 'C'}
]).done(function () {
Tag.findAll().done(callback)
})
},
userProducts: ['user', 'products', function (callback, results) {
results.user.setProducts(results.products).done(callback)
}],
productTags: ['products', 'tags', function (callback, results) {
var chainer = new Sequelize.Utils.QueryChainer()
chainer.add(results.products[0].setTags([results.tags[0], results.tags[2]]))
chainer.add(results.products[1].setTags([results.tags[1]]))
chainer.add(results.products[2].setTags([results.tags[0], results.tags[1], results.tags[2]]))
chainer.run().done(callback)
}]
}, function (err, results) {
expect(err).not.to.be.ok
User.find({
where: {
id: results.user.id
},
include: [
{model: Product, include: [
{model: Tag}
]}
]
}).done(function (err, user) {
expect(err).not.to.be.ok
expect(user.products.length).to.equal(4)
expect(user.products[0].tags.length).to.equal(2)
expect(user.products[1].tags.length).to.equal(1)
expect(user.products[2].tags.length).to.equal(3)
expect(user.products[3].tags.length).to.equal(0)
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!