如何使用 Sequelize 方法 fooInstance.createBar() 填充 n:m 关联的中间 table

How to fill the middle table of an n:m association with the methode fooInstance.createBar() with Sequelize

我想用sequelize提供的方法将数据填充到一个N:M拼缝table中。 由于我的数据在 allowNull: false 中,我无法在之后添加数据:/

这里是code/BDD的例子:

我的table一个:

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'
        });
    }

我的 table 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'
        });
    }

我的tablec:

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'
        });
    }

我希望能够做这样的事情:

fooAInstance.createB({somdeDataB: 15}, /* define here the data into JointedData */ {JointedData: 99});

我怎样才能实现这样的目标???

感谢您的时间和回答!

您应该像这样指明 through 选项:

await fooAInstance.createB({somdeDataB: 15}, { through: { JointedData: 99 } })

Advanced Many-to-Many