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

You need to sign in or sign up before continuing.
Commit 9fb47944 by Ricardo Lopes

Fixed how an included where is joined so it generates correct SQL code.

It now uses the field name instead of the model name.
1 parent 42b10a71
......@@ -1268,7 +1268,7 @@ var QueryGenerator = {
, subQueryWhere;
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 = {};
......
......@@ -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() {
var A = this.sequelize.define('a', {
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!