【发布时间】:2021-10-31 01:18:06
【问题描述】:
我有两个模型
- 文件
- 文档版本
这两个模型有两种不同类型的关联:
- 一个文档有多个文档版本:
Document.hasMany(models.DocumentVersion, {
foreignKey: { name: 'documentId', allowNull: false },
sourceKey: 'id',
onDelete: 'cascade',
as: 'documentVersion'
});
DocumentVersion.belongsTo(models.Document, {
foreignKey: { name: 'documentId', allowNull: false },
targetKey: 'id',
onDelete: 'cascade',
as: 'document'
});
在文档版本模型中创建 documentId 外键可以正常工作。
- 每个文档都有一个活动文档版本。所以文档模型应该有一个活动的 documentVersion Id。
我尝试在上述关联中添加以下内容:
Document.hasOne(models.DocumentVersion, {
foreignKey: { name: 'activeVersionId', allowNull: false },
targetKey: 'id',
as: 'activeDocumentVersion'
});
但这最终导致在 documentVersion 表中添加了 activeVersionId 而不是文档表。
如何添加第二个关联?
任何帮助将不胜感激。提前致谢。
【问题讨论】:
标签: node.js sequelize.js