Harshad BB
Harshad BB

Reputation: 119

How to use sequelize associations generated by sequelize auto

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.

Upvotes: 0

Views: 128

Answers (1)

Imran Azim
Imran Azim

Reputation: 174

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.

Upvotes: 1

Related Questions