sequelize.js:未处理的拒绝SequelizeForeignKeyConstraintError

时间:2017-07-22 00:51:49

标签: node.js postgresql express sequelize.js

我正在尝试使用以下函数将数据插入/更新到postgresql数据库中:

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);
  });
}

我已经看过我能找到的其他类似问题,但没有一个解决方案似乎有效。我一直收到以下错误。

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)

非常感谢任何帮助

0 个答案:

没有答案