我正在努力 Build 一个简单的n:m关系,案例是,一个医生可以有很多患者,患者可以有很多医生......

医生模型

module.exports = (sequelize, DataTypes) => {
  const Doctor = sequelize.define('Doctor', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    },
    doc_crm: {
      type: DataTypes.STRING,
      allowNull: false
    }
  }, {
    tableName: 'doctors',
    timestamps: false
  })

  Doctor.associate = function (models) {
    Doctor.belongsToMany(models.Patient, { as: 'Patients', through: { model: models.PatientDoctor } })
    Doctor.belongsTo(models.User, { foreignKey: 'user_id' })
  }

  return Doctor
}

患者模型

module.exports = (sequelize, DataTypes) => {
  const Patient = sequelize.define('Patient', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    }
  }, {
    tableName: 'patients',
    timestamps: false
  })

  Patient.associate = function (models) {
    Patient.belongsToMany(models.Doctor, { as: 'Doctors', through: { model: models.PatientDoctor } })
    Patient.belongsTo(models.User, { foreignKey: 'user_id' })
  }

  return Patient
}

加入表是

PatientDoctor

module.exports = (sequelize, DataTypes) => {
  const PatientDoctor = sequelize.define('PatientDoctor', {
    id: {
      type: DataTypes.INTEGER,
      primaryKey: true,
      autoIncrement: true
    }
  }, {
    tableName: 'patient_doctors',
    timestamps: false
  })
  return PatientDoctor
}

因此,当我查询通过患者和医生的PatientDoctor模型时,我得到了一个

{
    "name": "SequelizeEagerLoadingError"
}

我做错了什么?我尝试了很多东西,都没有运气 .

提前致谢!