Sequelize 多对多关系不作为外键保存在数据库中

Sequelize many to many relation are not saving in database as foreign keys

我有两个 tables campaignaudience ,我在它们之间设置多对多关系。 活动架构,

const Campaign = sequelize.define("campaign", {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
  },
  campaign_name: {
    type: DataTypes.STRING(50),
    allowNull: false,
  },
  text: {
    type: DataTypes.STRING(200),
    allowNull: false,
  },
  start_date: {
    type: DataTypes.INTEGER,
    allowNull: false,
  },
  end_date: {
    type: DataTypes.INTEGER,
    allowNull: false,
  },
});

受众架构,

const Audience = sequelize.define("audience", {
  id: {
    type: DataTypes.INTEGER,
    primaryKey: true,
    autoIncrement: true,
    allowNull: false,
  },
  audience_name: {
    type: DataTypes.STRING(50),
    allowNull: false,
  },
  phone: {
    type: DataTypes.STRING(12),
    allowNull: true,
  },
  email: {
    type: DataTypes.STRING(50),
    allowNull: true,
  },
});

然后我声明了多对多table campaign_audience,

const CampaignAudience = sequelize.define("campaign_audience", {
  audienceId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: Audience,
      key: "id",
    },
  },
  campaignId: {
    type: DataTypes.INTEGER,
    allowNull: false,
    references: {
      model: Campaign,
      key: "id",
    },
  },
});

然后设置它们之间的关系,

CampaignModel.belongsToMany(AudienceModel, {
  through: CampaignAudienceModel,
});
AudienceModel.belongsToMany(CampaignModel, {
  through: CampaignAudienceModel,
});

现在我已经创建了所有 table。问题是 campaignIdcampaign_audience table 中的 audienceId 不是外键。假设我有 ID 为 1 的活动和 ID 为 11,12 的受众。我可以 post id 为 43 的观众并将其保存在 table 中,这不是有效的观众 id。如何将联结 table id 设置为外键?

显然,如果我不手动声明 campaign_audience 架构,自动生成的字段将设置为外键。