Skip to content
Advertisement

Sequelize.js – “is not associated to”

I have some issue with getting full data from db. That are my models:

User

JavaScript

Role

JavaScript

I want to get object of one specific user including all role content. Somethink like

JavaScript

So I’m using:

JavaScript

but I get in the result err.message: “role is not associated to user”

And the simple question – what’s wrong ? 🙂

*to handle models I’m using sequelize-cli

Advertisement

Answer

You get this error because you didn’t add associate between the models

base on your json I see that each user only has one role, so you can either use belongsTo in role model or hasOne in user model

Should be something like this:

User.js

JavaScript

Role.js

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement