I want to fill the data into a N:M jointure table with the methods provided by sequelize.
As my data is in allowNull: false
, i can’t add the data afterward :/
Here an exemple of code/BDD:
my table a:
JavaScript
x
23
23
1
var A = sequelize.define('A', {
2
id: {
3
type: DataTypes.INTEGER,
4
primaryKey: true,
5
autoIncrement: true,
6
allowNull: false
7
},
8
someDataA: {
9
type: DataTypes.INTEGER,
10
allowNull: false
11
},
12
})
13
14
A.associate = function(models) {
15
A.belongsToMany(models.B, {
16
through: models.C,
17
foreignKey: 'a_id',
18
otherKey: 'b_id',
19
as: 'C'
20
});
21
}
22
23
my table b:
JavaScript
1
22
22
1
var B = sequelize.define('B', {
2
id: {
3
type: DataTypes.INTEGER,
4
primaryKey: true,
5
autoIncrement: true,
6
allowNull: false
7
},
8
someDataB: {
9
type: DataTypes.INTEGER,
10
allowNull: false
11
},
12
})
13
B.associate = function(models) {
14
B.belongsToMany(models.A, {
15
through: models.C,
16
foreignKey: 'b_id',
17
otherKey: 'a_id',
18
as: 'C'
19
});
20
}
21
22
my table c:
JavaScript
1
29
29
1
var C = sequelize.define('C', {
2
a_id: {
3
type: DataTypes.INTEGER,
4
primaryKey: true,
5
allowNull: false
6
},
7
b_id: {
8
type: DataTypes.INTEGER,
9
primaryKey: true,
10
allowNull: false
11
},
12
JointedData: {
13
type: DataTypes.INTEGER,
14
allowNull: false
15
},
16
})
17
18
C.associate = function(models) {
19
C.belongsTo(models.A, {
20
as: 'A',
21
foreignKey: 'a_id'
22
});
23
C.belongsTo(models.B, {
24
as: 'B',
25
foreignKey: 'b_id'
26
});
27
}
28
29
I want to be able to do something like this:
JavaScript
1
2
1
fooAInstance.createB({somdeDataB: 15}, /* define here the data into JointedData */ {JointedData: 99});
2
How can i achieve something like that???
Thx for your time and answers!
Advertisement
Answer
You should indicate through
option like this:
JavaScript
1
2
1
await fooAInstance.createB({somdeDataB: 15}, { through: { JointedData: 99 } })
2