[英]How to fill the middle table of an n:m association with the methode fooInstance.createBar() with Sequelize
I want to fill the data into a N:M jointure table with the methods provided by sequelize.我想用 sequelize 提供的方法将数据填充到 N:M 关节表中。 As my data is in
allowNull: false
, i can't add the data afterward:/由于我的数据在
allowNull: false
中,因此我无法在之后添加数据:/
Here an exemple of code/BDD:这是代码/ BDD的示例:
my table a:我的桌子:
var A = sequelize.define('A', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
someDataA: {
type: DataTypes.INTEGER,
allowNull: false
},
})
A.associate = function(models) {
A.belongsToMany(models.B, {
through: models.C,
foreignKey: 'a_id',
otherKey: 'b_id',
as: 'C'
});
}
my table b:我的桌子 b:
var B = sequelize.define('B', {
id: {
type: DataTypes.INTEGER,
primaryKey: true,
autoIncrement: true,
allowNull: false
},
someDataB: {
type: DataTypes.INTEGER,
allowNull: false
},
})
B.associate = function(models) {
B.belongsToMany(models.A, {
through: models.C,
foreignKey: 'b_id',
otherKey: 'a_id',
as: 'C'
});
}
my table c:我的桌子 c:
var C = sequelize.define('C', {
a_id: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false
},
b_id: {
type: DataTypes.INTEGER,
primaryKey: true,
allowNull: false
},
JointedData: {
type: DataTypes.INTEGER,
allowNull: false
},
})
C.associate = function(models) {
C.belongsTo(models.A, {
as: 'A',
foreignKey: 'a_id'
});
C.belongsTo(models.B, {
as: 'B',
foreignKey: 'b_id'
});
}
I want to be able to do something like this:我希望能够做这样的事情:
fooAInstance.createB({somdeDataB: 15}, /* define here the data into JointedData */ {JointedData: 99});
How can i achieve something like that???我怎样才能实现这样的目标???
Thx for your time and answers!谢谢你的时间和答案!
You should indicate through
option like this:您应该
through
这样的选项进行指示:
await fooAInstance.createB({somdeDataB: 15}, { through: { JointedData: 99 } })
See Advanced Many-to-Many请参阅 高级多对多
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.