sequelize.js: Unhandled rejection SequelizeForeignKeyConstraintError

Viewed 491

I am trying to insert/update data into a postgresql db using the following function:

var updateStudentDataAndReturn = function(req, res, { redirectLink: redirectLink, studentId: studentId }) {
  models.Student.update({
    firstName: req.body.firstName,
    lastName: req.body.lastName,
    phoneNumber: req.body.phoneNumber,
    skypeUsername: req.body.skypeUsername,
    typeOfStudent: req.body.typeOfStudent,
    school: req.body.school,
    expectedYearOfGraduation: req.body.expectedYearOfGraduation,
    TeamId: req.body.TeamId
  }, {
    where: {
      id: studentId
    }
  }).then(() => {
    return models.StudentMajor.destroy({
      where: {
        StudentId: studentId
      }
    })
  }).then(() => {
    return models.StudentSchool.destroy({
      where: {
        StudentId: studentId
      }
    })
  }).then(() => {
    if (!Array.isArray(req.body.major)) {req.body.major = [req.body.major]};
    return req.body.major.forEach((major) => {
      return models.StudentMajor.create({
        StudentId: studentId,
        major: major
      });
    });
  }).then(() => {
    if (!Array.isArray(req.body.school)) {req.body.school = [req.body.school]};
    return req.body.school.forEach((school) => {
      return models.StudentSchool.create({
        StudentId: studentId,
        school: school
      });
    });
  }).then(() => {
    return res.redirect(redirectLink);
  }).catch((err) => {
    console.log('ERROR', err);
  });
}

I've already looked at every other similar question I could find, but none of the solutions seem to work. I keep getting the following error.

Executing (default): INSERT INTO "StudentSchools" ("id","school","createdAt","updatedAt","StudentId") VALUES (DEFAULT,'sas','2017-07-22 00:41:49.763 +00:00','2017-07-22 00:41:49.763 +00:00',96) RETURNING *;
Unhandled rejection SequelizeForeignKeyConstraintError: insert or update on table "StudentMajors" violates foreign key constraint "StudentMajors_StudentId_fkey"
    at Query.formatError (/Users/Prathmesh/UPennTrackerDB/node_modules/sequelize/lib/dialects/postgres/query.js:296:14)
    at Result.<anonymous> (/Users/Prathmesh/UPennTrackerDB/node_modules/sequelize/lib/dialects/postgres/query.js:88:19)
    at emitOne (events.js:96:13)
    at Result.emit (events.js:188:7)
    at Result.Query.handleError (/Users/Prathmesh/UPennTrackerDB/node_modules/pg/lib/query.js:163:8)
    at Client.<anonymous> (/Users/Prathmesh/UPennTrackerDB/node_modules/pg/lib/client.js:188:26)
    at emitOne (events.js:96:13)
    at Connection.emit (events.js:188:7)
    at TLSSocket.<anonymous> (/Users/Prathmesh/UPennTrackerDB/node_modules/pg/lib/connection.js:133:12)
    at emitOne (events.js:96:13)
    at TLSSocket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at TLSSocket.Readable.push (_stream_readable.js:134:10)
    at TLSWrap.onread (net.js:543:20)

Executing (default): SELECT "id", "email", "hash", "salt", "role", "createdAt", "updatedAt" FROM "Users" AS "User" WHERE "User"."id" = 96;
Unhandled rejection SequelizeForeignKeyConstraintError: insert or update on table "StudentSchools" violates foreign key constraint "StudentSchools_StudentId_fkey"
    at Query.formatError (/Users/Prathmesh/UPennTrackerDB/node_modules/sequelize/lib/dialects/postgres/query.js:296:14)
    at Result.<anonymous> (/Users/Prathmesh/UPennTrackerDB/node_modules/sequelize/lib/dialects/postgres/query.js:88:19)
    at emitOne (events.js:96:13)
    at Result.emit (events.js:188:7)
    at Result.Query.handleError (/Users/Prathmesh/UPennTrackerDB/node_modules/pg/lib/query.js:163:8)
    at Client.<anonymous> (/Users/Prathmesh/UPennTrackerDB/node_modules/pg/lib/client.js:188:26)
    at emitOne (events.js:96:13)
    at Connection.emit (events.js:188:7)
    at TLSSocket.<anonymous> (/Users/Prathmesh/UPennTrackerDB/node_modules/pg/lib/connection.js:133:12)
    at emitOne (events.js:96:13)
    at TLSSocket.emit (events.js:188:7)
    at readableAddChunk (_stream_readable.js:176:18)
    at TLSSocket.Readable.push (_stream_readable.js:134:10)
    at TLSWrap.onread (net.js:543:20)

Any help would be much appreciated

0 Answers
Related