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

Commit 844657a0 by Mick Hansen

Fixes fixes fixes

1 parent 4a184db6
...@@ -1335,8 +1335,8 @@ module.exports = (function() { ...@@ -1335,8 +1335,8 @@ module.exports = (function() {
options.includeMap[include.as] = include options.includeMap[include.as] = include
options.includeNames.push(include.as) options.includeNames.push(include.as)
if (include.association.isMultiAssociation) options.hasMultiAssociation = true if (include.association.isMultiAssociation || include.hasMultiAssociation) options.hasMultiAssociation = true
if (include.association.isSingleAssociation) options.hasSingleAssociation = true if (include.association.isSingleAssociation || include.hasSingleAssociation) options.hasSingleAssociation = true
options.hasIncludeWhere = options.hasIncludeWhere || include.hasIncludeWhere || !!include.where options.hasIncludeWhere = options.hasIncludeWhere || include.hasIncludeWhere || !!include.where
options.hasIncludeRequired = options.hasIncludeRequired || include.hasIncludeRequired || !!include.required options.hasIncludeRequired = options.hasIncludeRequired || include.hasIncludeRequired || !!include.required
......
...@@ -473,8 +473,8 @@ module.exports = (function() { ...@@ -473,8 +473,8 @@ module.exports = (function() {
, mainQueryItems = [] , mainQueryItems = []
, mainAttributes = options.attributes , mainAttributes = options.attributes
, mainJoinQueries = [] , mainJoinQueries = []
// We'll use a subquery if we have hasMany associations and a limit or a filtered/required association // We'll use a subquery if we have hasMany associations and a limit and a filtered/required association
, subQuery = (limit || options.hasIncludeWhere || options.hasIncludeRequired) && options.hasMultiAssociation , subQuery = limit && (options.hasIncludeWhere || options.hasIncludeRequired || options.hasMultiAssociation)
, subQueryItems = [] , subQueryItems = []
, subQueryAttributes = null , subQueryAttributes = null
, subJoinQueries = [] , subJoinQueries = []
...@@ -484,6 +484,10 @@ module.exports = (function() { ...@@ -484,6 +484,10 @@ module.exports = (function() {
return this.quoteIdentifiers(t) return this.quoteIdentifiers(t)
}.bind(this)).join(", ") }.bind(this)).join(", ")
if (subQuery && mainAttributes) {
mainAttributes = mainAttributes.concat(factory.hasPrimaryKeys ? factory.primaryKeyAttributes : ['id'])
}
// Escape attributes // Escape attributes
mainAttributes = mainAttributes && mainAttributes.map(function(attr){ mainAttributes = mainAttributes && mainAttributes.map(function(attr){
var addTable = true var addTable = true
...@@ -518,6 +522,7 @@ module.exports = (function() { ...@@ -518,6 +522,7 @@ module.exports = (function() {
// If subquery, we ad the mainAttributes to the subQuery and set the mainAttributes to select * from subquery // If subquery, we ad the mainAttributes to the subQuery and set the mainAttributes to select * from subquery
if (subQuery) { if (subQuery) {
// We need primary keys
subQueryAttributes = mainAttributes subQueryAttributes = mainAttributes
mainAttributes = [options.table+'.*'] mainAttributes = [options.table+'.*']
} }
......
...@@ -237,8 +237,9 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -237,8 +237,9 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
self.User.find({ self.User.find({
where: { username: 'JohnXOXOXO' }, where: { username: 'JohnXOXOXO' },
attributes: ['username'] attributes: ['username']
}).success(function(user) { }).done(function(err, user) {
expect(user.selectedValues).to.have.property('username', 'JohnXOXOXO') expect(err).not.to.be.ok
expect(_.omit(user.selectedValues, ['id'])).to.have.property('username', 'JohnXOXOXO')
done() done()
}) })
}) })
...@@ -262,8 +263,9 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -262,8 +263,9 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
where: { username: 'John DOE' }, where: { username: 'John DOE' },
attributes: ['username'], attributes: ['username'],
include: [self.Mission] include: [self.Mission]
}).success(function(user) { }).done(function(err, user) {
expect(user.selectedValues).to.deep.equal({ username: 'John DOE' }) expect(err).not.to.be.ok
expect(_.omit(user.selectedValues, ['id'])).to.deep.equal({ username: 'John DOE' })
done() done()
}) })
}) })
...@@ -291,7 +293,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () { ...@@ -291,7 +293,7 @@ describe(Support.getTestDialectTeaser("DAOFactory"), function () {
attributes: ['username'], attributes: ['username'],
include: [{model: self.Mission, as: self.Mission.tableName, attributes: ['title']}] include: [{model: self.Mission, as: self.Mission.tableName, attributes: ['title']}]
}).success(function(user) { }).success(function(user) {
expect(user.selectedValues).to.deep.equal({ username: 'Brain Picker' }) expect(_.omit(user.selectedValues, ['id'])).to.deep.equal({ username: 'Brain Picker' })
expect(user.missions[0].selectedValues).to.deep.equal({ id: 1, title: 'another mission!!'}) expect(user.missions[0].selectedValues).to.deep.equal({ id: 1, title: 'another mission!!'})
expect(user.missions[0].foo).not.to.exist expect(user.missions[0].foo).not.to.exist
done() done()
......
...@@ -895,7 +895,9 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -895,7 +895,9 @@ describe(Support.getTestDialectTeaser("Include"), function () {
}) })
it('should be possible to extend the on clause with a where option on nested includes', function (done) { it('should be possible to extend the on clause with a where option on nested includes', function (done) {
var User = this.sequelize.define('User', {}) var User = this.sequelize.define('User', {
name: DataTypes.STRING
})
, Product = this.sequelize.define('Product', { , Product = this.sequelize.define('Product', {
title: DataTypes.STRING title: DataTypes.STRING
}) })
...@@ -984,7 +986,7 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -984,7 +986,7 @@ describe(Support.getTestDialectTeaser("Include"), function () {
async.auto({ async.auto({
user: function (callback) { user: function (callback) {
User.create().done(callback) User.create({name: 'FooBarzz'}).done(callback)
}, },
memberships: ['user', function (callback, results) { memberships: ['user', function (callback, results) {
GroupMember.bulkCreate([ GroupMember.bulkCreate([
...@@ -1123,22 +1125,26 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -1123,22 +1125,26 @@ describe(Support.getTestDialectTeaser("Include"), function () {
}) })
}) })
it('should be possible use limit and a where on a belongsTo with additional hasMany includes', function (done) { it('should be possible use limit, attributes and a where on a belongsTo with additional hasMany includes', function (done) {
var self = this var self = this
this.fixtureA(function () { this.fixtureA(function () {
self.models.Product.findAll({ self.models.Product.findAll({
attributes: ['title'],
include: [ include: [
{model: self.models.Company, where: {name: 'NYSE'}}, {model: self.models.Company, where: {name: 'NYSE'}},
{model: self.models.Tag}, {model: self.models.Tag},
{model: self.models.Price} {model: self.models.Price}
], ],
limit: 3, limit: 3,
order: 'id ASC' order: [
[self.sequelize.col(self.models.Product.tableName+'.id'), 'ASC']
]
}).done(function (err, products) { }).done(function (err, products) {
expect(err).not.to.be.ok expect(err).not.to.be.ok
expect(products.length).to.equal(3) expect(products.length).to.equal(3)
products.forEach(function (product) { products.forEach(function (product) {
expect(product.company.name).to.equal('NYSE')
expect(product.tags.length).to.be.ok expect(product.tags.length).to.be.ok
expect(product.prices.length).to.be.ok expect(product.prices.length).to.be.ok
}) })
...@@ -1151,6 +1157,7 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -1151,6 +1157,7 @@ describe(Support.getTestDialectTeaser("Include"), function () {
var self = this var self = this
this.fixtureA(function () { this.fixtureA(function () {
self.models.Product.findAll({ self.models.Product.findAll({
include: [ include: [
{model: self.models.Company}, {model: self.models.Company},
{model: self.models.Tag}, {model: self.models.Tag},
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!