Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

christopher's avatar

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

This isn't directly Laravel related, but SQL. Maybe someone can help me out. I am getting the above error if i try to insert a new entry to the table.

The category_id and category_version_id which i am trying to insert into my new news table are already existing within the category table. The columns are id and version_id within the category table.

So i don't understand why i am getting the error.

** SQL query **

INSERT INTO `development`.`news` (`id`, `teaser`, `category_id`, `category_version_id`) VALUES ('1', 'dasad', '4550C07D5EBF40BEA01F7A9351D1373C', '0FA91CE3E96A4BC2BE4BD9CE752C3425');

** Error **

Cannot add or update a child row: a foreign key constraint fails (`development`.`news`, CONSTRAINT `fk.news.category_id` FOREIGN KEY (`category_id`, `category_version_id`) REFERENCES `category` (`id`, `version_id`) ON DELETE CASCADE ON UPDATE CASCADE)

** News Table **

CREATE TABLE IF NOT EXISTS `news` (
                `id` BINARY(16) NOT NULL,
                `teaser` VARCHAR(255) NOT NULL,
                `category_id` BINARY(16) NOT NULL,
                `category_version_id` BINARY(16) NOT NULL,
                `created_at` DATETIME(3) NOT NULL,
                `updated_at` DATETIME(3) NULL,
                PRIMARY KEY (`id`)
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

second migration for the foreign keys

ALTER TABLE `news`
       ADD KEY `fk.news.category_id` (`category_id`,`category_version_id`),
       ADD CONSTRAINT `news.category_id` FOREIGN KEY (`category_id`,`category_version_id`) REFERENCES `category` (`id`,`version_id`) ON DELETE CASCADE ON UPDATE CASCADE
0 likes
6 replies
Tray2's avatar

You need to insert the category_id into the category table first.

2 likes
christopher's avatar

@tray2 Within the category table the id 4550C07D5EBF40BEA01F7A9351D1373C and version_id 0FA91CE3E96A4BC2BE4BD9CE752C3425 already exist.

So this is the already existing category, which i am trying to reference within my newly created table news.

category table: The id and version_id already exist within the category table.

Structure of my news table:

kalemdzievski's avatar

Wow, this is really weird. Since i cannot see the actual implementation i would recommend to double checkking the following things (even tho by looking at the info u shared, everything seems to be fine):

  1. Both tables have the same engine (I can see that this is okay)
  2. Database has the same engine (InnoDB)
  3. Both columns have same data types in both tables (I can see that this is okay)
  4. Both values for category_id and category_version_id exist in the category table (I can see that this is okay)

I would suggest maybe try truncating both tables and try inserting again? (But I doubt it would help)

1 like
christopher's avatar
christopher
OP
Best Answer
Level 30

The problem was UNHEX while inserting a string.

Please or to participate in this conversation.