【发布时间】:2017-11-05 01:17:35
【问题描述】:
有没有办法与直通表进行多态自关联(例如,集合具有并属于许多集合)?
尝试使http://docs.sequelizejs.com/manual/tutorial/associations.html#n-m 适应这种情况:
// Inside collection.js associate
Collection.belongsToMany(Collection, {
through: {
model: models.CollectionItem,
unique: false,
scope: {
collectible: 'collection'
}
},
foreignKey: 'collectibleUid',
constraints: false
});
collectionItem.js 的样子
const CollectionItem = sequelize.define("CollectionItem", {
uid: {
type: DataTypes.BIGINT,
primaryKey: true
},
collectionUid: {
type: DataTypes.BIGINT,
allowNull: false,
unique: 'collection_item_collectible'
},
order: {
type: DataTypes.INTEGER,
allowNull: false,
defaultValue: 0
},
collectibleUid: {
type: DataTypes.BIGINT,
allowNull: false,
references: null, // Because the column is polymorphic, we cannot say that it REFERENCES a specific table
unique: 'collection_item_collectible'
},
collectible: {
type: DataTypes.STRING,
allowNull: false,
unique: 'collection_item_collectible'
}
}, {
classMethods: {
}
});
似乎 Sequelize 希望我通过另一个连接/通过表来命名它,但这本质上是创建一个新表,而不是仅仅获得一个真正的 hasAndBelongsToMany 类型关系。
错误:必须为多对多自关联定义“as”
【问题讨论】:
-
如错误所说,m:n 自关系需要
as参数,尝试定义它
标签: node.js database sequelize.js has-and-belongs-to-many polymorphic-associations