New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Many To Many Association Fetch Not Working #3185

Closed
grimabe opened this Issue Feb 20, 2015 · 7 comments

Comments

2 participants
@grimabe

grimabe commented Feb 20, 2015

I am using a many to many association between a user and a group and I got the following error when I want to fetch the groups associated to a user :

Possibly unhandled SequelizeDatabaseError: la colonne User_has_Group.GroupIdGroup n'existe pas
    at module.exports.Query.formatError (.../node_modules/sequelize/lib/dialects/postgres/query.js:361:16)
    at null.<anonymous> (.../node_modules/sequelize/lib/dialects/postgres/query.js:79:21)
    at emit (events.js:95:17)
    at Query.handleError (.../node_modules/pg/lib/query.js:99:8)
    at null.<anonymous> (.../node_modules/pg/lib/client.js:166:26)
    at emit (events.js:95:17)
    at Socket.<anonymous> (.../node_modules/pg/lib/connection.js:109:12)
    at Socket.emit (events.js:95:17)
    at Socket.<anonymous> (_stream_readable.js:765:14)
    at Socket.emit (events.js:92:17)

Environment :

  • PostgreSQL 9.4
  • Node v0.10.36
  • Sequelize v2.0.3

Here is the code :

User model

var User = sequelize.define('User', {
        'iduser': {
            type: DataTypes.UUID,
            primaryKey: true,
            defaultValue: DataTypes.UUIDV4,
            allowNull: false
        },
        ....
    }, {
        tableName: 'User',
        paranoid: true,
        // Timestamps
        timestamps: true,
        createdAt: 'created_at',
        updatedAt: false,
        deletedAt: 'deleted_at',
        // Methods
        classMethods: {
            associate: function(models) {
                User.belongsToMany(models.Group, {as:'groups', through: models.User_has_Group, foreignKey: 'id_user'});
            }
        }
    });

Group Model

var Group = sequelize.define('Group', {
        'id_group': {
            type: DataTypes.UUID,
            allowNull: false,
            primaryKey: true,
            defaultValue: DataTypes.UUIDV4
        },
        ....
    }, {
        tableName: 'Group',
        paranoid: true,
        // Timestamps
        timestamps: true,
        createdAt: 'created_at',
        updatedAt: false,
        deletedAt: 'deleted_at',
        // Methods
        classMethods: {
            associate: function(models) {
                Group.belongsToMany(models.User, {as: 'users', through: models.User_has_Group, foreignKey: 'id_group'});
            }
        }
    });

User_has_Group Model

    var User_has_Group = sequelize.define('User_has_Group', {
        ...
    }, {
        tableName: 'User_has_Group',
        paranoid: false,
        // Timestamps
        timestamps: true,
        createdAt: 'created_at',
        updatedAt: 'updated_at',
        deletedAt: 'deleted_at'
    });

The code that crash
on the line _user.getGroups().then(function(groups) {_

Models.User.find({
      where: {
        id_user: request.params.id_user
      }
    })
      .then(function(user) {
          ...
          user.getGroups().then(function(groups) {

          })
        }
      })

Thanks for the help

@mickhansen mickhansen added the bug label Feb 21, 2015

@mickhansen

This comment has been minimized.

Contributor

mickhansen commented Feb 21, 2015

Appears to be a bug, can you log Object.keys(User_has_Group.rawAttributes) and show it here?

@grimabe

This comment has been minimized.

grimabe commented Feb 21, 2015

[ ....
  'created_at',
  'updated_at',
  'deleted_at',
  'id_group',
  'id_user' ]

the ... are attributes that are not relevant for the issue. (I suppose)

@mickhansen

This comment has been minimized.

Contributor

mickhansen commented Feb 21, 2015

GroupIdGroup does not appear to be in that list so it might be :)
Can you show the SQL for the getGroups query being run?

@grimabe

This comment has been minimized.

grimabe commented Feb 22, 2015

Sure :

SELECT "Group"."id_group",
       "Group"."group",
       "Group"."created_at",
       "Group"."deleted_at",
       "User_has_Group"."created_at" AS "User_has_Group.created_at",
       "User_has_Group"."updated_at" AS "User_has_Group.updated_at",
       "User_has_Group"."deleted_at" AS "User_has_Group.deleted_at",
       "User_has_Group"."id_group" AS "User_has_Group.id_group",
       "User_has_Group"."id_user" AS "User_has_Group.id_user"
FROM "Group" AS "Group"
INNER JOIN "User_has_Group" AS "User_has_Group" ON "Group"."id_group" = "User_has_Group"."GroupIdGroup"
AND ("User_has_Group"."deleted_at" IS NULL
     AND "User_has_Group"."id_user" = '6a9d3226-d83c-42dc-8969-af1a92cdca65')
WHERE ("Group"."deleted_at" IS NULL);

Thanks for the help

@mickhansen

This comment has been minimized.

Contributor

mickhansen commented Feb 22, 2015

Hmm, you're hitting a bug i was sure i fixed, for now you can probably work around with:

User.belongsToMany(models.Group, {as:'groups', through: models.User_has_Group, foreignKey: 'id_user', otherKey: 'id_group'});
Group.belongsToMany(models.User, {as:'users', through: models.User_has_Group, foreignKey: 'id_group', otherKey: 'id_user'});
@grimabe

This comment has been minimized.

grimabe commented Feb 22, 2015

Thanks ! your work around works.
But I'm still interested to know the final solution :D

@mickhansen

This comment has been minimized.

Contributor

mickhansen commented Feb 22, 2015

@grimabe It should work with what you have, there must be a bug :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment