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

Commit e1ce0e21 by Mick Hansen

Merge pull request #1695 from cusspvz/master

subQuery Check is using sourceTable on a through Select
2 parents 530d0102 b9f61180
......@@ -737,25 +737,26 @@ module.exports = (function() {
joinQueryItem += sourceJoinOn
// Generate join SQL for right side of through
joinQueryItem += joinType + self.quoteTable(table, as) + " ON "
joinQueryItem += joinType + self.quoteTable( table, as ) + " ON "
joinQueryItem += targetJoinOn
if (include.where) {
if ( include.where ) {
targetWhere = self.getWhereConditions(include.where, self.sequelize.literal(self.quoteIdentifier(as)), include.daoFactory, whereOptions)
joinQueryItem += " AND "+ targetWhere
if (subQuery) {
if (!options.where) options.where = {}
// Creating the as-is where for the subQuery, checks that the required association exists
var _where = "(";
_where += "SELECT "+self.quoteIdentifier(identSource)+" FROM " + self.quoteTable(throughTable, throughAs);
_where += joinType + self.quoteTable(table, as) + " ON "+targetJoinOn;
_where += " WHERE " + sourceJoinOn + " AND " + targetWhere + " LIMIT 1"
_where += ")";
_where += " IS NOT NULL"
options.where["__"+throughAs] = self.sequelize.asIs(_where)
options.where["__"+throughAs] = self.sequelize.asIs([ '(',
"SELECT " + self.quoteIdentifier( throughAs ) + "." + self.quoteIdentifier( identSource ) + " FROM " + self.quoteTable(throughTable, throughAs),
! include.required && joinType + self.quoteTable( association.source.tableName, tableSource ) + " ON " + sourceJoinOn || '',
joinType + self.quoteTable( table, as ) + " ON " + targetJoinOn,
"WHERE " + ( ! include.required && targetWhere || sourceJoinOn + " AND " + targetWhere ),
"LIMIT 1",
')', 'IS NOT NULL'].join(' '))
}
}
} else {
......
/* jshint camelcase: false */
/* jshint expr: true */
var chai = require('chai')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + "/../../lib/data-types")
, datetime = require('chai-datetime')
chai.use(datetime)
chai.config.includeStack = true
describe(Support.getTestDialectTeaser("Include"), function () {
describe('find', function () {
it( 'Try to include a non required model, with conditions and two includes N:M 1:M', function ( done ) {
var DT = DataTypes,
S = this.sequelize,
A = S.define('A', { name: DT.STRING(40) }, { paranoid: true }),
B = S.define('B', { name: DT.STRING(40) }, { paranoid: true }),
C = S.define('C', { name: DT.STRING(40) }, { paranoid: true }),
D = S.define('D', { name: DT.STRING(40) }, { paranoid: true })
// Associations
A
.hasMany( B )
B
.belongsTo( B )
.belongsTo( D )
.hasMany( C, {
through: 'BC',
})
C
.hasMany( B, {
through: 'BC',
})
D
.hasMany( B )
S.sync({ force: true }).done( function ( err ) { expect( err ).not.to.be.ok
A.find({
include: [
{ model: B, required: false, include: [
{ model: C, required: false },
{ model: D }
]}
]
}).done( function ( err ) {
expect( err ).not.to.be.ok
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!