如Sequelize
文档中所述,
foo.belongstomany(Bar,{through:Baz})
那么有一个方法可以插入到连接表中:
FooInstance.AddBars([5,4])
它将在Baz
连接表中插入两个字段:FOOID,BARID
。但我还需要插入另一个字段值。像这样的事:
FooInstance.AddBars([{barid:5,otherField:'xxx'},...]
不需要手动插入,如何才能实现?
请参阅高级多对多指南。
const User_Profile = sequelize.define('User_Profile', {
selfGranted: DataTypes.BOOLEAN
}, { timestamps: false });
User.belongsToMany(Profile, { through: User_Profile });
Profile.belongsToMany(User, { through: User_Profile });
有了这个,我们现在可以跟踪直通表中的一个额外信息,即自授布尔值。例如,当调用user.addProfile()时,我们可以使用through选项传递额外列的值。
Example:
const amidala = await User.create({ username: 'p4dm3', points: 1000 });
const queen = await Profile.create({ name: 'Queen' });
await amidala.addProfile(queen, { through: { selfGranted: false } });