Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot add or update a child row: a foreign key constraint fails #15

Open
Lopastudio opened this issue Jul 21, 2023 · 0 comments
Open

Comments

@Lopastudio
Copy link

I made this API acording to the tutorial, and after starting it up, I started getting this error:
Cannot add or update a child row: a foreign key constraint fails (ne028304db.user_roles, CONSTRAINT user_roles_ibfk_1 FOREIGN KEY (roleId) REFERENCES roles (id) ON DELETE CASCADE ON UPDATE CASCADE)
It only occurs when I try to register and the api crashes.
Here is a full error:

node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

Error
    at Query.run (B:\code\Projects\Quizztopia\backend\Quizztopia\AuthServer\node_modules\sequelize\lib\dialects\mysql\query.js:52:25)
    at B:\code\Projects\Quizztopia\backend\Quizztopia\AuthServer\node_modules\sequelize\lib\sequelize.js:315:28
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async MySQLQueryInterface.bulkInsert (B:\code\Projects\Quizztopia\backend\Quizztopia\AuthServer\node_modules\sequelize\lib\dialects\abstract\query-interface.js:346:21)
    at async recursiveBulkCreate (B:\code\Projects\Quizztopia\backend\Quizztopia\AuthServer\node_modules\sequelize\lib\model.js:1697:25)
    at async user_roles.bulkCreate (B:\code\Projects\Quizztopia\backend\Quizztopia\AuthServer\node_modules\sequelize\lib\model.js:1786:12)
    at async Promise.all (index 0)
    at async BelongsToMany.set (B:\code\Projects\Quizztopia\backend\Quizztopia\AuthServer\node_modules\sequelize\lib\associations\belongs-to-many.js:452:14) {
  name: 'SequelizeForeignKeyConstraintError',
  parent: Error: Cannot add or update a child row: a foreign key constraint fails (`ne028304db`.`user_roles`, CONSTRAINT `user_roles_ibfk_1` FOREIGN KEY (`roleId`) REFERENCES `roles` (`id`) ON DELETE CASCADE ON UPDATE CASCADE) 
      at Packet.asError (B:\code\Projects\Quizztopia\backend\Quizztopia\AuthServer\node_modules\mysql2\lib\packets\packet.js:728:17)
      at Query.execute (B:\code\Projects\Quizztopia\backend\Quizztopia\AuthServer\node_modules\mysql2\lib\commands\command.js:29:26)
      at Connection.handlePacket (B:\code\Projects\Quizztopia\backend\Quizztopia\AuthServer\node_modules\mysql2\lib\connection.js:456:32)
      at PacketParser.onPacket (B:\code\Projects\Quizztopia\backend\Quizztopia\AuthServer\node_modules\mysql2\lib\connection.js:85:12)
      at PacketParser.executeStart (B:\code\Projects\Quizztopia\backend\Quizztopia\AuthServer\node_modules\mysql2\lib\packet_parser.js:75:16)
      at Socket.<anonymous> (B:\code\Projects\Quizztopia\backend\Quizztopia\AuthServer\node_modules\mysql2\lib\connection.js:92:25)
      at Socket.emit (node:events:513:28)
      at addChunk (node:internal/streams/readable:324:12)
      at readableAddChunk (node:internal/streams/readable:297:9)
      at Readable.push (node:internal/streams/readable:234:10) {
    code: 'ER_NO_REFERENCED_ROW_2',
    errno: 1452,
    sqlState: '23000',
    sqlMessage: 'Cannot add or update a child row: a foreign key constraint fails (`ne028304db`.`user_roles`, CONSTRAINT `user_roles_ibfk_1` FOREIGN KEY (`roleId`) REFERENCES `roles` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)',
    sql: "INSERT INTO `user_roles` (`createdAt`,`updatedAt`,`roleId`,`userId`) VALUES ('2023-07-21 07:45:14','2023-07-21 07:45:14',1,7);",
    parameters: undefined
  },
  original: Error: Cannot add or update a child row: a foreign key constraint fails (`ne028304db`.`user_roles`, CONSTRAINT `user_roles_ibfk_1` FOREIGN KEY (`roleId`) REFERENCES `roles` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
      at Packet.asError (B:\code\Projects\Quizztopia\backend\Quizztopia\AuthServer\node_modules\mysql2\lib\packets\packet.js:728:17)
      at Query.execute (B:\code\Projects\Quizztopia\backend\Quizztopia\AuthServer\node_modules\mysql2\lib\commands\command.js:29:26)
      at Connection.handlePacket (B:\code\Projects\Quizztopia\backend\Quizztopia\AuthServer\node_modules\mysql2\lib\connection.js:456:32)
      at PacketParser.onPacket (B:\code\Projects\Quizztopia\backend\Quizztopia\AuthServer\node_modules\mysql2\lib\connection.js:85:12)
      at PacketParser.executeStart (B:\code\Projects\Quizztopia\backend\Quizztopia\AuthServer\node_modules\mysql2\lib\packet_parser.js:75:16)
      at Socket.<anonymous> (B:\code\Projects\Quizztopia\backend\Quizztopia\AuthServer\node_modules\mysql2\lib\connection.js:92:25)
      at Socket.emit (node:events:513:28)
      at addChunk (node:internal/streams/readable:324:12)
      at readableAddChunk (node:internal/streams/readable:297:9)
      at Readable.push (node:internal/streams/readable:234:10) {
    code: 'ER_NO_REFERENCED_ROW_2',
    errno: 1452,
    sqlState: '23000',
    sqlMessage: 'Cannot add or update a child row: a foreign key constraint fails (`ne028304db`.`user_roles`, CONSTRAINT `user_roles_ibfk_1` FOREIGN KEY (`roleId`) REFERENCES `roles` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)',
    sql: "INSERT INTO `user_roles` (`createdAt`,`updatedAt`,`roleId`,`userId`) VALUES ('2023-07-21 07:45:14','2023-07-21 07:45:14',1,7);",
    parameters: undefined
  },
  sql: "INSERT INTO `user_roles` (`createdAt`,`updatedAt`,`roleId`,`userId`) VALUES ('2023-07-21 07:45:14','2023-07-21 07:45:14',1,7);",
  parameters: {},
  table: 'roles',
  fields: [ 'roleId' ],
  value: undefined,
  index: 'user_roles_ibfk_1',
  reltype: 'child'
}

Node.js v18.15.0

Thanks to everyone :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant