Sequelize - create recond with value that matches another table foreign key's value

Viewed 23

I have the following structure for Users_Role table:

| role_id(PK)    | role_name     |
| ---------------| --------------|
| 1              | Admin         |
| 2              | View          |

And the following structure for Users table:

| user_id (PK) | user_role_id(FK)    |
| -------------| --------------------|
| 12345        | 1                   |
| 22434        | 1                   |

The tables are connected by user_role_id and role_id I want to create new records in Users Table, the thing is I only have the user_role value.

Is there a way to shortcut the way to get the value of the user_role_id, or the only way to do it is a seperate query before creating new record in Users?

const userRole = "Admin";

Users.create({
        user_id:"1234",
        user_role_id: ???
      })
0 Answers
Related