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

Commit 1824379a by Mick Hansen

Merge pull request #1655 from cusspvz/master

Fix generation of eager loading for childs on required and conditionable wheres
2 parents 15feacc1 8667b5a7
......@@ -763,13 +763,20 @@ module.exports = (function() {
// Filter statement
// Used by both join and subquery where
if (subQuery && !include.subQuery && include.parent.subQuery) {
where = self.quoteIdentifier(tableLeft + "." + attrLeft) + " = "
} else {
where = self.quoteTable(tableLeft) + "." + self.quoteIdentifier(attrLeft) + " = "
}
where += self.quoteTable(tableRight) + "." + self.quoteIdentifier(attrRight)
where =
// Left side
(
( subQuery && !include.subQuery && include.parent.subQuery && !( include.hasParentRequired && include.hasParentWhere ) ) && self.quoteIdentifier(tableLeft + "." + attrLeft) ||
self.quoteTable(tableLeft) + "." + self.quoteIdentifier(attrLeft)
)
+ " = " +
// Right side
(
( subQuery && !include.subQuery && include.parent.subQuery && ( include.hasParentRequired && include.hasParentWhere ) ) && self.quoteIdentifier(tableRight + "." + attrRight) ||
self.quoteTable(tableRight) + "." + self.quoteIdentifier(attrRight)
)
// Generate join SQL
joinQueryItem += joinType + self.quoteTable(table, as) + " ON "
......
......@@ -821,6 +821,51 @@ describe(Support.getTestDialectTeaser("Include"), function () {
})
})
it("should be possible to define a belongsTo include as required with child hasMany not required", function(done) {
var S = this.sequelize
, Address = S.define('Address', { 'active': DataTypes.BOOLEAN })
, Street = S.define('Street', { 'active': DataTypes.BOOLEAN })
, User = S.define('User', { 'username': DataTypes.STRING })
// Associate
User.belongsTo( Address, { foreignKey: 'addressId' })
Address.hasMany( User, { foreignKey: 'addressId' })
Address.belongsTo( Street, { foreignKey: 'streetId' })
Street.hasMany( Address, { foreignKey: 'streetId' })
// Sync
S.sync({ force: true }).success(function() {
// Create instances
Street.create({ active: true }).done(function ( err, street ){ expect(err).not.to.be.ok; expect(street).to.be.ok
Address.create({ active: true, streetId: street.id }).done(function ( err, address ){ expect(err).not.to.be.ok; expect(address).to.be.ok
User.create({ username: 'John', addressId: address.id }).done(function ( err, john ){ expect(err).not.to.be.ok; expect(john).to.be.ok
// Test
User.find({
where: { username: 'John'},
include: [
{ model: Address, include: [
{ model: Street }
], required: true, where: { active: true } }
]
}).done(function (err, john) {
expect(err).not.to.be.ok
expect(john.address).to.be.ok
expect(john.address.street).to.be.ok
done();
})
})
})
})
})
})
it('should be possible to define a belongsTo include as required with child hasMany with limit', function (done) {
var User = this.sequelize.define('User', {})
, Group = this.sequelize.define('Group', {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!