I'm new to Sequelize. Current working on a small schema, needed to add a foreign key to a table, and found the following:
sequelize.define(
"my_model", {
"id": {
type: Sequelize.UUID,
primaryKey: true,
defaultValue: Sequelize.UUIDV4
},
...
"linked_model": {
type: Sequelize.UUID,
// Should I be using this form to create foreign keys?
references: {
model: MyOtherModel,
key: id
}
}
}
)
A little later, I found out about the associations API - http://docs.sequelizejs.com/manual/tutorial/associations.html - Which can also be used to create foreign keys on related tables:
const Player = this.sequelize.define('player', {/* attributes */});
const Team = this.sequelize.define('team', {/* attributes */});
Player.belongsTo(Team); // Will add a teamId attribute to Player to hold the primary key value for Team
The same question also appears here - Sequelize model references vs associations
Which of the two is considered best practice?