How to use sequelize associations generated by sequelize auto

106 views Asked by At

I have something like this in init-models.js

USERS.belongsTo(ROLES, { as: "user_role_ROLE", foreignKey: "user_role"});
  ROLES.hasMany(USERS, { as: "USERs", foreignKey: "user_role"});

If I directly use them in resolver it throws me an error called "You have used the alias in two separate associations. Aliased associations must have unique aliases"

so how to use them in resolver files.

1

There are 1 answers

0
Imran Azim On

In your role.js model, keep the associations in the bottom of the file before the module.exports.

ROLES.hasMany(USERS, { as: "users", foreignKey: "user_role" }); // OR foreignKey: "role_id"
USERS.belongsTo(ROLES, { as: "role", foreignKey: "user_role" }); // OR foreignKey: "role_id"

I hope this help.