1

I have two models Doctor and Degree with many to many relationship.

Doctor model:

    const Doctor = _sequelize.define('doctor', {
    fullname: {
        type: Sequelize.STRING,
        allowNull: false
    },
    email: {
        type: Sequelize.STRING,
        allowNull: false
    },
    password: {
        type: Sequelize.STRING,
        allowNull: false
    },
    gender: {
        type: Sequelize.STRING
    },
    address: {
        type: Sequelize.STRING
    },
    phone: {
        type: Sequelize.STRING
    },
    avatarurl: {
        type: Sequelize.STRING
    },
    active: {
        type: Sequelize.BOOLEAN,
        defaultValue: true
    },
    license: {
        type: Sequelize.STRING
    }
    },{
        timestamps: false,
        tableName: 'doctor'
        })
    Doctor.associate = (models) => {
        Doctor.belongsToMany(model.degree, { through: 'doctor_degree', foreignKey: 'doctor_id',as: 'degree' })
    }

Degree model:

    const Degree = _sequelize.define('degree', {
    name: {
        type: Sequelize.STRING
    }
    },{
        timestamps: false,
        tableName: 'degree'
        })
    Degree.associate = (models) => {
        Degree.belongsToMany(models.doctor, { through: 'doctor_degree', foreignKey: 'degree_id' })
    };   

Doctor_Degree model:

    const Doctor_Degree = _sequelize.define('doctor_degree', {
    doctor_id: {
      type: Sequelize.STRING,
      allowNull: false,
      references: {
        model: 'doctor',
        key: 'id'
      }
    },
    degree_id: {
      type: Sequelize.STRING,
      allowNull: false,
      references: {
        model: 'degree',
        key: 'id'
      }
    }
  },{
    timestamps: false,
    tableName: 'doctor_degree'
    });

And I have a service to find all doctor with degree

    var result;
    try{
        await doctorModel.findAll({
            attributes: ['id','fullname', 'email','gender','address','phone','avatarurl','active','license'],
            include: [{
                model: degreeModel,
                as: 'degree'
              }]
            }).then((doctors) => {
            result = doctors
        })
    }
    catch(err){
        throw err
    }
    return result

But i got the following error: [SequelizeEagerLoadingError]: degree is not associated to doctor!

Can anyone suggest what I am doing wrong?

William Prigol Lopes
  • 1,803
  • 14
  • 31
TanIkemen
  • 53
  • 1
  • 7
  • Can you check `doctorModel` associations prop at the line `await doctorModel.findAll` by setting a breakpoint? – Anatoly Jun 08 '20 at 18:34
  • I try to call doctorModel.associate(), but it throw err 'column degree->doctor_degree.degreeId does not exist'. – TanIkemen Jun 09 '20 at 00:52
  • I called degreeModel.associate() then it throw that 'degree.belongsToMany called with something that's not a subclass of Sequelize.Model' – TanIkemen Jun 09 '20 at 00:54
  • you have error in association ? you can define with foriegnkey with doctor & degree modal i think you don't have to definr docter_degree modal . – Arpit Vyas Jun 09 '20 at 03:37
  • At least add `otherKey: 'degree_id'` to `Doctor.belongsToMany(model.degree, { through: 'doctor_degree', foreignKey: 'doctor_id',as: 'degree' })` – Anatoly Jun 09 '20 at 18:52
  • Also please register all models and only after that register all associations like I recommend in this answer https://stackoverflow.com/a/61710568/1376618 – Anatoly Jun 09 '20 at 18:54

0 Answers0