Sequelize hasAssociation default query

13 views
Skip to first unread message

Chinonso Eke

unread,
Sep 27, 2022, 9:21:21 PM9/27/22
to Sequelize
Hello I have a many to many relationship between user and roles

I'm trying to create an instance method to check if a user hasRoles based on an array of role names (strings) passed to it

async checkHasRole(roles) {
            return await this.hasRoles(roles);
        }

however this runs this query 

SELECT `Role`.`id` FROM `Roles` AS `Role` INNER JOIN `role_user` AS `Role_User` ON `Role`.`id` = `Role_User`.`RoleId` AND `Role_User`.`UserId` = 231 WHERE (((`Role`.`id` = 'staff')))

It's referencing the Role.id.

Please how can I change this to reference the Role.name and use an IN operator instead and completely scrap out the ID

I tried importing the Role Model in the User class model but running findAll returns undefined.

Please help, thanks

Reply all
Reply to author
Forward
0 new messages