Skip to content

Many To Many Association Fetch Not Working #3185

@grimabe

Description

@grimabe

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugDEPRECATED: replace with the "bug" issue type

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions