Home > Back-end >  how to relational data one-to-one and many-to-many in sequalize in model same?
how to relational data one-to-one and many-to-many in sequalize in model same?

Time:12-29

I have a problem in findById data with relational sequelize. Where I have the condition of the One-to-One and Many-to-Many relations

  1. TB_User is related on-to-one with tb_corporate
  2. TB_User is related to many-to-many with TB Corporate and this data relations are saved in TB_corporate_Group

My problem, when I want to get corporate and corporate group. The data I get is data from the user table where the relationship is one-to-one.

this is code in model users

    module.exports = (sequelize :any ) => {
  class users extends Model{
    static associate(models : any) {
      // define association here
      users.belongsToMany(models.corporate, {
        through: 'corporateGroup',
        as: 'dataCorporateGroup'
      })
      users.hasOne(models.corporate, {foreignKey: 'userId', as: 'corporate'})
      
    }
  }
  users.init({
    id: {
      type: DataTypes.INTEGER,
      allowNull: false,
      primaryKey: true,
      autoIncrement: true
    },
    name: {
      field: 'name',
      type: DataTypes.STRING,
      allowNull: false,
    },
  }, {
    sequelize,
    modelName: 'users',
  });
  return users;
};

code for model corporate

module.exports = (sequelize :any ) => {
  class corporate extends Model{
    static associate(models : any) {
      // define association here
      corporate.belongsTo(models.users, {foreignKey: 'userId', as: 'userCorporate'})
      corporate.belongsToMany(models.users, {
        through: 'corporateGroup',
        as: 'userCorporate'
      })
    }
  }
  corporate.init({
    userId: {
      field: 'user_id',
      type: DataTypes.INTEGER,
      allowNull: false,
      unique: true,
    },
    corporateName: {
      field: 'name',
      type: DataTypes.STRING,
      allowNull: false,
      unique: true,
    },
  }, {
    sequelize,
    modelName: 'corporate',
  });
  return corporate;
};

code for model corporate group

module.exports = (sequelize :any ) => {
  class corporateGroup extends Model{
    static associate(models : any) {
      // define association here
      
    }
  }
  corporateGroup.init({
    corporateId: {
      field: 'corporate_id',
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'corporate',
        key: 'id'
      }
    },
    userId: {
      field: 'user_id',
      type: DataTypes.INTEGER,
      allowNull: false,
      references: {
        model: 'users',
        key: 'id'
      }
    }
  }, {
    sequelize,
    tableName: 'corporate_group',
    modelName: 'corporateGroup',
  });
  return corporateGroup;
};

and this code for find by id

await corporate.findById(id, {
  include: [{
    model: db.users,
    as: 'userCorporate',
  }]
})

CodePudding user response:

You need to indicate the unique aliases for associations

corporate.belongsTo(models.users, {foreignKey: 'userId', as: 'userCorporate'})
corporate.belongsToMany(models.users, {
        through: 'corporateGroup',
        as: 'corporateGroupUsers'
      })

and then you can indicate included models with these aliases to get both of them along with corporate record:

await corporate.findById(id, {
  include: [{
    model: db.users,
    as: 'userCorporate',
  }, {
    model: db.users,
    as: 'corporateGroupUsers',
  }]
})
  • Related