Repository navigation
Closed
Description
I have the following models with a belongsToMany relation:
var User = sequelize.define("User", {
firstname: DataTypes.STRING,
lastname: DataTypes.STRING,
username: DataTypes.STRING,
password: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
User.belongsToMany(models.Role, { as:'roles', through: 'UserRoles'} );
}
}
}); var Role = sequelize.define("Role", {
name: DataTypes.STRING,
description: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
Role.belongsToMany(models.User, { as:'users', through: 'UserRoles'} );
}
}
});Then I excute this and pass the result to the web framework I'm using (Hapi.js in my case):
models.User.findAll({ include: [{ model:models.Role, as:'roles' }] })The following json resulted:
[
{
"id": 1,
"firstname": "Miguel",
"lastname": "Andrade",
"username": "miguelcobain",
"password": "dirtylittlesecret",
"createdAt": "2015-01-20T12:49:40.331Z",
"updatedAt": "2015-01-20T12:49:40.331Z",
"roles": [
{
"id": 1,
"name": "admin",
"description": "Administrator",
"createdAt": "2015-01-20T12:50:46.139Z",
"updatedAt": "2015-01-20T12:50:46.139Z",
"UserRoles": {
"createdAt": "2015-01-20T12:51:34.046Z",
"updatedAt": "2015-01-20T12:51:34.046Z",
"UserId": 1,
"RoleId": 1
}
}
]
}
]The SQL query that is executed is:
SELECT "User"."id",
"User"."firstname",
"User"."lastname",
"User"."username",
"User"."password",
"User"."createdAt",
"User"."updatedAt",
"roles"."id" AS "roles.id",
"roles"."name" AS "roles.name",
"roles"."description" AS "roles.description",
"roles"."createdAt" AS "roles.createdAt",
"roles"."updatedAt" AS "roles.updatedAt",
"roles.UserRoles"."createdAt" AS "roles.UserRoles.createdAt",
"roles.UserRoles"."updatedAt" AS "roles.UserRoles.updatedAt",
"roles.UserRoles"."UserId" AS "roles.UserRoles.UserId",
"roles.UserRoles"."RoleId" AS "roles.UserRoles.RoleId"
FROM "Users" AS "User"
LEFT OUTER JOIN (
"UserRoles" AS "roles.UserRoles"
LEFT OUTER JOIN "Roles" AS "roles" ON "roles"."id" = "roles.UserRoles"."RoleId"
) ON "User"."id" = "roles.UserRoles"."UserId";What I'm not understanding is how the UserRoles object ended up on the eager loaded roles array.
I'm using PostgreSQL, postgres dialect, "pg": "^3.6.3" and"sequelize": "^2.0.0-rc7".
Metadata
Metadata
Assignees
Labels
No labels
Activity
mickhansen commentedon Jan 20, 2015
That's how join table attributes are included. You can remove the completely by using
include: [{ model:models.Role, as:'roles', through: {attributes: []} }]mickhansen commentedon Jan 20, 2015
The reason is that we don't currently distinguish between join tables that only contain foreign keys and join tables that contain actual values.
miguelcobain commentedon Jan 20, 2015
That indeed worked, thanks for the quick reply!
I'll just leave here my POV on this matter:
Thanks again.
mickhansen commentedon Jan 20, 2015
Yeah i agree.
I agree aswell, but that's a major change that can be achieved by using the join model as an include (so not a big priority to change since it can be achieved trivially already)
miguelcobain commentedon Jan 20, 2015
Also, there aren't any docs on this
throughproperty onincludes, right?I would document it, but I didn't understand it, honestly.
mickhansen commentedon Jan 20, 2015
@miguelcobain hmm possibly not, we can be quite bad at not having the docs follow the code.
himanshugogroup commentedon Mar 9, 2024
Using
sequelize: "^6.36.0"Below code works for me by having zero attribute selection in association but using it in where clause so only attribute of main model will be in select but as in sql we ca select main table attribute but where clause which run before select can use filter.