Sequelize Node JS中的BelongsToMany关联转换

问题描述

我正在处理一个项目,该项目有 3 个使用 BelongsToMany 关联连接的表。由于某些原因,我想在Sequelize中将其转换为1-M和1-1关系。

凭证模型:

    const Credential = sequelize.define(
        "credentials",{
          password: DataTypes.STRING,site: DataTypes.STRING,last_fetched_at: {
            type: DataTypes.DATE,allowNull: true
          },consecutive_failures_count: {
            type: DataTypes.INTEGER,allowNull: true,defaultValue: 0
          },card_type_id: {
            type: DataTypes.INTEGER
          },locked: {
            type: DataTypes.VIRTUAL(DataTypes.BOOLEAN,["locked_at"]),get: function () {
              if (!this.get("locked_at")) return false
              return this.get("locked_at") > Date.Now() - (60 * 60000)
            }
          },prefer_card_type: {
            type: DataTypes.STRING
          },purchasing_account_id: {
            type: DataTypes.INTEGER,allowNull: false
          },error: {
            type: DataTypes.TEXT
          }
        },{
          indexes: [
            {
              unique: true,fields: ["site","purchasing_account_id"]
            }
          ],underscored: true,freezeTableName: true,updatedAt: "updated_at",createdAt: "created_at"
        }
      )


Credential.associate = function (models) {
  /*
  Credential.belongsToMany(models.cc_details,{
    through: 'cc_details_credentials',foreignKey: 'credentials_id',otherKey: 'cc_id',constraints: false,});
  */
  Credential.hasMany(models.cc_details_credentials,{
    foreignKey: 'credentials_id',onDelete: 'CASCADE',});
};

信用卡模型:

const CCDetails = sequelize.define(
    "cc_details",{
      card_no: {
        type: DataTypes.STRING(30),allowNull: false,unique: true
      },card_holder_name: {
        type: DataTypes.STRING(50),allowNull: false
      },card_expiry_month: {
        type: DataTypes.STRING(15),allowNull: true
      },primary: {
        type: DataTypes.BOOLEAN,defaultValue: false
      },last_four: {
        type: DataTypes.STRING(15)
      },card_type_id: {
        type: DataTypes.INTEGER
      },state: {
        type: DataTypes.STRING(30)
      },country: {
        type: DataTypes.STRING(30)
      },zip: {
        type: DataTypes.STRING(15)
      },},{
      underscored: true,createdAt: "created_at"
    }
  )
CCDetails.associate = function (models) {
  /* If i uncomment hasOne relation and comment BelongsToMany relation then it will give an error
  CCDetails.hasOne(models.cc_details_credentials,{
    foreignKey: 'cc_id',});
  */
  CCDetails.belongsToMany(models.credentials,foreignKey: 'cc_id',otherKey: 'credentials_id',});
};

CC_Details_Credentials(加入表):

const ccDetailsCredentials = sequelize.define(
    "cc_details_credentials",{
      cc_id: { type: DataTypes.INTEGER,allowNull: false },credentials_id: { type: DataTypes.INTEGER,createdAt: "created_at"
    }
  )
ccDetailsCredentials.associate = function (models) {
  ccDetailsCredentials.belongsTo(models.credentials,});
  ccDetailsCredentials.belongsTo(models.cc_details,{
    foreignKey: 'cc_id'
  });
};

CC_Details Credentials 只有两个字段,即 Credentials Id(来自 Credentials 表)和 CC Id(来自信用卡表)

我使用 hasMany 关联成功创建了凭据和 cc_details_credentials 表之间的关系。

但是当我想在 cc_details_credentials 和 cc_details 表之间创建 1-1 关系时,它会给我一个错误,即连接表(cc_details_credentials)中不存在 id 字段。而不是它,如果我使用belongsToMany 关联保持不变,那么它将正确给出结果。 我不明白这背后的逻辑,为什么它在一侧起作用,而在另一侧不起作用。

Credentials -> CC_Details_Credentials is working Fine

CC_Details_Credentials -> CC_Details is not working Fine

我还尝试使用连接表上的 hasOne 关系和 CC_details 表上的belongsTo 建立关系 1-1。但这也不起作用。

"error": "'field list' 中的未知列 'credentials->cc_details_credentials.id'",

寻求您的帮助。谢谢

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)