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

Commit bf185d94 by Mick Hansen

Support required option on includes

1 parent 92a7bac7
......@@ -1337,6 +1337,10 @@ module.exports = (function() {
include.include.push(include.through)
}
if (include.required === undefined) {
}
// Validate child includes
if (include.hasOwnProperty('include')) {
validateIncludedElements(include)
......
......@@ -493,6 +493,7 @@ module.exports = (function() {
, attributes
, association = include.association
, through = include.through
, joinType = include.required ? ' INNER JOIN ' : ' LEFT OUTER JOIN '
if (tableName !== parentTable) as = parentTable+'.'+include.as
......@@ -521,11 +522,11 @@ module.exports = (function() {
, identTarget = association.foreignIdentifier
, attrTarget = ((!include.association.target.hasPrimaryKeys || primaryKeysTarget.length !== 1) ? 'id' : primaryKeysTarget[0])
joinQueryItem += " LEFT OUTER JOIN " + self.quoteIdentifier(throughTable) + " AS " + self.quoteIdentifier(throughAs) + " ON "
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 += " LEFT OUTER JOIN " + self.quoteIdentifier(table) + " AS " + self.quoteIdentifier(as) + " ON "
joinQueryItem += joinType + self.quoteIdentifier(table) + " AS " + self.quoteIdentifier(as) + " ON "
joinQueryItem += self.quoteIdentifier(tableTarget) + "." + self.quoteIdentifier(attrTarget) + " = "
joinQueryItem += self.quoteIdentifier(throughAs) + "." + self.quoteIdentifier(identTarget)
} else {
......@@ -535,7 +536,9 @@ module.exports = (function() {
, tableRight = ((association.associationType === 'BelongsTo') ? parentTable : as)
, attrRight = association.identifier
joinQueryItem += " LEFT OUTER JOIN " + self.quoteIdentifier(table) + " AS " + self.quoteIdentifier(as) + " ON " + self.quoteIdentifier(tableLeft) + "." + self.quoteIdentifier(attrLeft) + " = " + self.quoteIdentifier(tableRight) + "." + self.quoteIdentifier(attrRight)
joinQueryItem += joinType + self.quoteIdentifier(table) + " AS " + self.quoteIdentifier(as) + " ON "
joinQueryItem += self.quoteIdentifier(tableLeft) + "." + self.quoteIdentifier(attrLeft) + " = "
joinQueryItem += self.quoteIdentifier(tableRight) + "." + self.quoteIdentifier(attrRight)
}
if (include.include) {
......
......@@ -890,7 +890,7 @@ describe(Support.getTestDialectTeaser("Include"), function () {
})
})
it('should support through models with attributes', function (done) {
it('should include attributes from through models', function (done) {
var Product = this.sequelize.define('Product', {
title: DataTypes.STRING
})
......@@ -967,6 +967,46 @@ describe(Support.getTestDialectTeaser("Include"), function () {
})
})
it('should support a required belongsTo include', function (done) {
var User = this.sequelize.define('User', {})
, Group = this.sequelize.define('group', {})
User.belongsTo(Group)
this.sequelize.sync({force: true}).done(function () {
async.auto({
groups: function (callback) {
Group.bulkCreate([{}, {}]).done(function () {
Group.findAll().done(callback)
})
},
users: function (callback) {
User.bulkCreate([{}, {}, {}]).done(function () {
User.findAll().done(callback)
})
},
userGroups: ['users', 'groups', function (callback, results) {
results.users[2].setGroup(results.groups[1]).done(callback)
}]
}, function (err, results) {
expect(err).not.to.be.ok
User.findAll({
include: [
{model: Group, required: true}
]
}).done(function (err, users) {
expect(err).not.to.be.ok
expect(users.length).to.equal(1)
expect(users[0].group).to.be.ok
done()
})
})
})
})
})
describe('findAndCountAll', function () {
xit('should include associations to findAndCountAll', function(done) {
var User = this.sequelize.define('User', {})
, Item = this.sequelize.define('Item', {'test': DataTypes.STRING})
......@@ -1009,12 +1049,9 @@ describe(Support.getTestDialectTeaser("Include"), function () {
expect(result.rows.length).to.eql(1)
expect(result.rows[0].item.test).to.eql('def')
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!