0

Using Sequelize with MySQL. I have three models. Consultant, FamilyMember and Appointments. Appointment refers to Consultant and FamilyMember.

I have defined the foreign keys in the Appointment model. When the DB is created - the foreign keys are visible - when I check through a MySQL client, on the appointment table. The table names are freeze - so there isn't any chance of pluralization of the table names.

Consultant Model:

module.exports = (sequelize, DataTypes) => {
const consultant = sequelize.define('consultant', {
  ID: {
    type: DataTypes.UUID,
    primaryKey: true,
    allowNull: false
  },
  FirstName: {
    type: DataTypes.STRING,
    allowNull: false
  },
  LastName: {
    type: DataTypes.STRING,
    allowNull: false
  }
{
  freezeTableName: true 
}
);

return consultant;
};

Appointment Model:

module.exports = (sequelize, DataTypes) => {
const appointment = sequelize.define('appointment', {
  // attributes
  ID: {
    type: DataTypes.UUID,
    primaryKey: true,
    allowNull: false
  },
  ConsultantID: {
    type: DataTypes.UUID,
    allowNull: false,
    references: {         
      model: 'consultant',
      key: 'ID'
    }
  },
  FamilyMemberID: {
    type: DataTypes.UUID,
    allowNull: false,
    references: {         
      model: 'familymember',
      key: 'ID'
    }
  }
}, 
{
  freezeTableName: true 
}
);

appointment.associate = function (models) {
  models.appointment.belongsTo(models.consultant, {
    foreignKey: 'ConsultantID',
    as: 'consultant',
  });
  models.appointment.belongsTo(models.familymember, {
    foreignKey: 'FamilyMemberID',
    as: 'familymember',
  });
};

return appointment;
};

Family Member model:

module.exports = (sequelize, DataTypes) => {
const familymember = sequelize.define('familymember', {
  // attributes
  ID: {
    primaryKey: true,
    type: DataTypes.UUID,
    allowNull: false
  },
  FamilyID: {
    type: DataTypes.UUID,
    allowNull: false
  },
  FirstName: {
    type: DataTypes.STRING,
    allowNull: false
  },
  LastName: {
    type: DataTypes.STRING,
    allowNull: false
  }
}, 
{
  freezeTableName: true 
}
);
return familymember;
};

Then in the code I try to fetch appointment and get the related familymember and consultant like this

    var appointments = await Appointment.findAll({
        where: {
            AppointmentDateConfirmed: {
                $gte: moment().subtract(0, 'days').toDate()
              }
        }, include:[Consultant, FamilyMember]
    }
    )

However I get an error

UnhandledPromiseRejectionWarning: SequelizeEagerLoadingError: consultant is not associated to appointment!

General Grievance
  • 4,555
  • 31
  • 31
  • 45
KManish
  • 1,551
  • 2
  • 11
  • 7
  • Show a code where you register all associations from models. It seems Sequelize does not see your associations. – Anatoly Jun 30 '20 at 17:04
  • @Anatoly - I couldn't format in the comment well. I have updated the main query above - Update 2.0. It has the code the way I am doing it now. – KManish Jun 30 '20 at 17:46

1 Answers1

1

I suppose you should register your associations after models registration like I pointed in this answer

Anatoly
  • 20,799
  • 3
  • 28
  • 42
  • Great. Trying it right now. Also found another of your answers - that will be helpful in this case [similar scope](https://stackoverflow.com/questions/61605988/how-to-add-more-field-in-joined-table-belongstomany-in-sequelize/61609315#61609315) . I think in my case this will be required as well - since I am defining the association table manually and adding more attributes to it. – KManish Jun 30 '20 at 19:06
  • I made the necessary changes as per the post [answer](https://stackoverflow.com/a/61710568/1376618) - and the question - Update 3.0 gives the db.js file now. There is an error next. Any idea - what it could be? I tried setting the `code` db.sequelize = sequelize; db.Sequelize = Sequelize; `code` before the fs loop. But no luck. – KManish Jun 30 '20 at 20:20
  • 1
    You also should define models as functions see a model definition in the question https://stackoverflow.com/a/61710568/1376618 – Anatoly Jun 30 '20 at 20:30
  • Thanks. Just finished doing that. Also moved the `code` Consultant.belongsToMany(FamilyMember, { through: appointment }) FamilyMember.belongsToMany(Consultant, { through: appointment }) before -- `code` return appointment - in the appointment model, as otherwise appointment couldn't be found. Now the error is that belongsToMany is not a function. – KManish Jun 30 '20 at 20:58
  • Please update the main post with corrected models and associations – Anatoly Jul 01 '20 at 06:59
  • Thanks a lot. I have updated the schema for the three models. Based upon another of your discussion https://stackoverflow.com/questions/61709290/ I updated the way the db.js and index.js were syncing and working. So all the previous errors are gone. And Sequelize registers all the models successfully. However now before I could define the association/relation - the consultant table is throwing error in creation - seems like the primaryKey attribute is being ignored by the Sequelize. – KManish Jul 01 '20 at 09:09
  • It seems you didn't move association definitions to models or you didn't include them in the post – Anatoly Jul 01 '20 at 13:03
  • SOLVED. @Anatoly - Thanks Anatoly. Working as expected. I must say it can be little tough the first time. But thanks to you and all. Finally worked. Updated the association definitions in case someone needs it. Unfortunately I am in such an early stage on StackOverflow that I can upvote [which I have done] - but seems it isn't counted yet. Sorry for that. – KManish Jul 02 '20 at 07:33