3

I'm using sequelize to model a mySql-database schema in my node-application. Let's say I have 3 table: Project, User and Role.

It's a "Many to Many" association between Project and User through "Project_User" where is defined the role of a user for a project.

Project Model :

var Project = sequelize.define('Project', {
    name:{type: DataTypes.STRING, unique: true}
},
classMethods: {
   associate: function(models) {
       Project.belongsToMany(models.User, { through: 'Project_User', as: 'users'});
   }
}
// Methods...
);

User Model :

var User= sequelize.define('User', {
    name:{type: DataTypes.STRING, unique: true}
},
classMethods: {
   associate: function(models) {
       User.belongsToMany(models.Project, { through: 'Project_User', as: 'projects'});
   }
}
// Methods...
);

And here is the association table Project_User Model :

var Project_User = sequelize.define('Project_User', {
    role:
    {
        type: DataTypes.INTEGER,
        allowNull: false,
        references: 'Role',
        referencesKey: 'id'
    }
},{
    classMethods: {
        associate: function(models) {
            Project_User.belongsTo(models.Role, {foreignKey: 'role'});
        }
    }
});

Now, I want to find all project, with their users and their role. So I've used findAll with the "include" parameters like below:

models.Projects.findAll({
    include:[
             {
                 model: models.User, 
                 as:'users',
                 through: {attributes: ['role'], as: 'role'}
             }]
}).then(function(result) {
     // ...
});

This works great but I only have the roleId associated to the user. I wasn't be able to link this "roleId" with the role table to get the other attributes like role name...

Here is the JSON I've got :

[
{
    "id": 1,
    "name": "Project name",
    "users": [
        {
            "id": 1,
            "name": "User name",
            "role": {
                "role": 1
            }
        }
    ]
}
]

But I would like to have something like that :

[
{
    "id": 1,
    "name": "Project name",
    "users": [
        {
            "id": 1,
            "name": "User name",
            "role": {
                "id": 1,
                "name": "Role name",
                "description": "Some info...",
            }
        }
    ]
}
]

I've tried many things to realize this association, even successive includes but it was unsuccessful. What is needed in the findAll options to get this JSON result ?

Thanks

1 Answer 1

2

Assuming that your User model is linked to the Role model, something like this should work:

models.Projects.findAll({
    include:[
             {
                 model: models.User, 
                 as:'users',
                 through: {attributes: []},
                 include: [models.Role]
             }] }).then(function(result) {
     // ... });
4
  • Thanks to you reply but I've already try and it doesn't works. Indeed, I've got an error like "Unknown column users.role" which makes sense because role isn't a column from users table but in project_user.
    – Raptack
    Jun 16, 2016 at 13:11
  • If you don't have a relation between User and Role then your only option is to model the join table (project_user) and add the relation to Role there. You can't retrieve full model data (a row of a table) without defining an association...
    – Aralizer
    Jun 16, 2016 at 19:12
  • 1
    Yes that's what I've done. You can see my Projet_User model, with a relation to Role (roleId as foreign key). But I haven't found the correct syntax in my findAll() to display role data and not only the foreign key roleId for each user associated to the project.
    – Raptack
    Jun 16, 2016 at 20:01
  • 1
    @Raptack it would be awesome if you got a solution now and you can share it with us, I have the same problem now... thanks
    – DariusV
    Apr 6, 2019 at 17:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.