0

I have two models defined:

db/models/User.js

const Sequelize = require('sequelize');

module.exports = (sequelize, DataTypes) => {
  class User extends Sequelize.Model {}

  const config = {
    sequelize,
    modelName: 'user',
    underscored: true,
    paranoid: true,
  });

  User.init({
    first_name: {
      type: DataTypes.STRING,
    },
    last_name: {
      type: DataTypes.STRING,
    },
  }, config);

  return User;
};

db/models/Group.js

const Sequelize = require('sequelize');

module.exports = (sequelize, DataTypes) => {
  class Group extends Sequelize.Model {}

  const config = {
    sequelize,
    modelName: 'group',
    underscored: true,
    paranoid: true,
  });

  Group.init({
    name: {
      type: DataTypes.STRING,
      allowNull: false,
    },
  }, config);

  return Group;
};

I want to create many-to-many association between then (so a group can have many users, and a user can belong to many groups. Here is my db/index.js:

db/index.js

const Sequelize = require('sequelize');
const Logger = require('../config/logger');
const { DB } = require('../config/vars');
const UserModel = require('./models/User');
const GroupModel = require('./models/Group');

const sequelize = new Sequelize(
  DB.name,
  DB.username,
  DB.password,
  {
    host: DB.host,
    dialect: 'postgres',
  },
);

const Models = {
  User: UserModel(sequelize, Sequelize),
  Group: GroupModel(sequelize, Sequelize),
};

const setup = async () => {
  // Associations
  Models.User.belongsToMany(Models.Group, {
    through: 'UserGroups',
    as: { singular: 'group', plural: 'groups' },
  });
  Models.Group.belongsToMany(Models.User, {
    through: 'UserGroups',
    as: { singuler: 'user', plural: 'users' },
  });
  await sequelize.sync({ force: true });
};

module.exports = {
  ...Models,
  setup,
  Seeders,
  Sequelize,
  sequelize,
};

When I do User.findAll(), there is no groups attribute returned in the object. How should I properly set up many-to-many relationship here?

mdmb
  • 4,833
  • 7
  • 42
  • 90

1 Answers1

0

User.findAll() will return it associated models by defaults, you need to use include like this :

User.findAll({
    include : {
        model : Groups 
    }
})

SIMPLE GUIDE

Vivek Doshi
  • 56,649
  • 12
  • 110
  • 122
  • I'm getting an error saying `"group is not associated to user!",` – mdmb Aug 18 '19 at 19:36
  • @Ancinek, means you haven't define association properly. – Vivek Doshi Aug 18 '19 at 19:36
  • – please have a look at the updated code above. I removed my own ids and added `singular` and `plural` when defining them. – mdmb Aug 18 '19 at 19:38
  • try `Models.User.hasMany(Models.Group);` this. – Vivek Doshi Aug 18 '19 at 19:41
  • Then `Error: N:M associations are not supported with hasMany. Use belongsToMany instead` appears – mdmb Aug 18 '19 at 19:42
  • change this also `Models.Group.belongsTo(Models.User);` – Vivek Doshi Aug 18 '19 at 19:44
  • Wouldn't it make this many-to-one instead of many-to-many in this case? Those two `belongsToMany` correctly create a `user_groups` join table that I can lookg for in using `psql`, but somehow there is a problem while querying. – mdmb Aug 18 '19 at 19:45
  • 1
    if your association is correct then my query should work, do read : https://stackoverflow.com/questions/22958683/how-to-implement-many-to-many-association-in-sequelize – Vivek Doshi Aug 18 '19 at 19:49
  • Check this also : https://stackoverflow.com/questions/43419514/sequelize-join-models-include-many-to-many – Vivek Doshi Aug 18 '19 at 19:50
  • Unfortunately, still same error `"group is not associated to user!"`. The problem is probably that those answers are not related to v5 of sequelize. – mdmb Aug 18 '19 at 19:57