LaCarl
LaCarl

Reputation: 105

belongsTo Or hasOne Sequelize

I have 2 tables, Users and Profile_Education. Users data is coming from an auth0/login form, and Profile_Education is coming from an API using node.js/express. I want Profile_education to have a foreign key to track data and display it in a profile Based on the user who logs in.

In my project, should I use belongsTo or hasOne, or should I use both?

Upvotes: 0

Views: 782

Answers (1)

Vivek Doshi
Vivek Doshi

Reputation: 58543

You can use both of them but use it where it suitable , as per the DOC :

When information about association is present in source model we can use belongsTo. In this case Player is suitable for belongsTo because it has teamId column.

Player.belongsTo(Team)  // `teamId` will be added on Player / Source model

When information about association is present in target model we can use hasOne. In this case Coach is suitable for hasOne because Team model store information about its Coach as coachId field.

Coach.hasOne(Team)  // `coachId` will be added on Team / Target model

I think this will clear all your doubts ,

Upvotes: 2

Related Questions