我正在播放一个带有续集装置的数据库,我已经知道如何处理与它之间的多对多关系:
Project.hasMany(User);
User.hasMany(Project);
[
{
"model":"User",
"data":{
"id": 123,
"name": "John"
}
},
{
"model":"Project",
"data": {
"id": 20,
"name": "The Great Project",
"UserProjects": [123]
}
}
]
但是,如果连接表具有其他属性,例如:
,该怎么办?User = sequelize.define('user', {})
Project = sequelize.define('project', {})
UserProjects = sequelize.define('userProjects', {
status: DataTypes.STRING
})
User.belongsToMany(Project, { through: UserProjects })
Project.belongsToMany(User, { through: UserProjects })
如果我使用上面的夹具,加载夹具后状态将为null,因为它不考虑status属性。如何使用sequelize-fixtures为连接表UserProjects的status属性设定种子?
答案 0 :(得分:1)
现在可以使用特殊的_through
密钥为m2m关系在v.1.5.1的sequelize-fixtures中完成:
[
{
"model":"User",
"data":{
"id": 123,
"name": "John"
}
},
{
"model":"Project",
"data": {
"id": 20,
"name": "The Great Project",
"users":[
{
"id": 123,
"_through": {
"status": "active"
}
}
]
}
}
]