[英]What is the foreign key in a one-to-many self join in sequelize?
我有一個Institution
模型,其中一個機構可以屬於一個Parent Institution
,我將其設置為institutions
表上的自聯接。
我正在使用sequelize
在 postgres 上進行設置,文檔討論如何為多對多關聯設置自聯接,但我的模型使用一對多自聯接(即父機構可以有很多孩子,但一個孩子機構只能屬於一個家長)。
這是我的模型:
module.exports = (sequelize, DataTypes) => {
const Institution = sequelize.define(
'Institution',
{
/* ... */
parentId: DataTypes.INTEGER,
/* ... */
},
{}
)
Institution.associate = function (models) {
// associations can be defined here
Institution.belongsTo(Institution, {
foreignKey: 'institutionId',
as: 'parent',
})
}
return Institution
}
我不確定的是在上面的關聯foreignKey
什么作為外foreignKey
- 它應該是我現在放置的institutionId
foreignKey
(即使用as: parent
參數)還是應該是parentId
?
提前致謝!
它應該是parentId因為這個字段是對父機構的引用
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.