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

Commit 156c3dbe by Mick Hansen

Merge pull request #4532 from Americas/joinfix

Fixed how an included where is joined so it generates correct SQL code.
2 parents 1a272c85 9fb47944
...@@ -1268,7 +1268,7 @@ var QueryGenerator = { ...@@ -1268,7 +1268,7 @@ var QueryGenerator = {
, subQueryWhere; , subQueryWhere;
associationWhere[association.identifierField] = { associationWhere[association.identifierField] = {
$raw: self.quoteTable(parentTable) + '.' + self.quoteIdentifier(association.source.primaryKeyAttribute) $raw: self.quoteTable(parentTable) + '.' + self.quoteIdentifier(association.source.primaryKeyField)
}; };
if (!options.where) options.where = {}; if (!options.where) options.where = {};
......
...@@ -97,6 +97,43 @@ describe(Support.getTestDialectTeaser('Include'), function() { ...@@ -97,6 +97,43 @@ describe(Support.getTestDialectTeaser('Include'), function() {
}); });
}); });
it('should include a model with a where clause when the PK field name and attribute name are different', function() {
var User = this.sequelize.define('User', {
id: {
type: DataTypes.UUID,
defaultValue: Sequelize.UUIDV4,
field: 'main_id',
primaryKey: true
}
})
, Task = this.sequelize.define('Task', {
searchString: { type: DataTypes.STRING }
});
User.hasMany(Task, {foreignKey: 'userId'});
Task.belongsTo(User, {foreignKey: 'userId'});
return this.sequelize.sync({
force: true
}).then(function() {
return User.create();
}).then(function(user) {
return Task.bulkCreate([
{userId: user.get('id'), searchString: 'one'},
{userId: user.get('id'), searchString: 'two'},
]);
}).then(function() {
return User.find({
include: [
{model: Task, where: {searchString: 'one'} }
]
});
}).then(function(user) {
expect(user).to.be.ok;
expect(user.Tasks.length).to.equal(1);
});
});
it('should still pull the main record when an included model is not required and has where restrictions without matches', function() { it('should still pull the main record when an included model is not required and has where restrictions without matches', function() {
var A = this.sequelize.define('a', { var A = this.sequelize.define('a', {
name: DataTypes.STRING(40) name: DataTypes.STRING(40)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!