我是Sequelize的新手。当前正在处理一个小模式,需要将一个外键添加到表中,并找到以下内容:
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
}
}
}
)
过了一会儿,我发现了关联API - http://docs.sequelizejs.com/manual/tutorial/associations.html - 它也可用于在相关表上创建外键:
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
同样的问题也出现在这里 - Sequelize model references vs associations
这两者中的哪一个被认为是最佳做法?