REFERENCES ticket_replies (ticket_id) ??
How have you defined the foreign constraint and why ????
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
I've got a table called tickets. design looks like: http://puu.sh/mAKLK/3e3eb90428.png
The other table called ticket_replies looks like: http://puu.sh/mAKQC/7f24cd0c1a.png
The "tickets" table contains a foreign key to the "ticket_replies" table. This foreign key is constructed like this: http://puu.sh/mAL2Z/44aa5c39bd.png
In my controller I want to use this code:
//create new ticket
$ticket = new Ticket;
$ticket->user_id = $user->id;
$ticket->title = $request->title;
$ticket->status = 0;
$ticket->department_id = $request->departments;
$ticket->save();
//create new ticket_replie
$ticket_replie = new Ticket_replie;
$ticket_replie->ticket_id = $ticket->id;
$ticket_replie->user_id = $user->id;
$ticket_replie->text = $request->question;
$ticket_replie->save();
This results in an error saying:
Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (sampleauth.tickets, CONSTRAINT tickets_ibfk_1 FOREIGN KEY (id) REFERENCES ticket_replies (ticket_id) ON DELETE CASCADE ON UPDATE CASCADE) (SQL: insert into tickets (user_id, title, status, department_id, updated_at, created_at) values (1, sasa, 0, 1, 2016-01-18 23:03:21, 2016-01-18 23:03:21))
I can understand why this occurs but as you may see on my table design, there isn't a way to prevent this, because I need the "generated" id of ticket in my ticket_replie. -Any help desirabled ;)
Please or to participate in this conversation.