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

**URL:** https://forum.shopware.com/t/cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails/66052
**Category:** Programmierung
**Created:** [5. April 2020 um 07:32 UTC](https://forum.shopware.com/t/cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails/66052 "2020-04-05T07:32:54Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![Shopwareianer](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@Shopwareianer](https://forum.shopware.com/u/Shopwareianer)
#### Post date: [5. April 2020 um 07:32 UTC](https://forum.shopware.com/t/cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails/66052/1 "2020-04-05T07:32:54Z")

</div>

Ich hänge seit Stunden fest und kann mir den Fehler nicht erklären:&nbsp; Ich habe eine neue Table welche mit der category table connected werden soll. Hierzu habe ich in meiner custom table die category\_id und category\_version\_id Spalte, welche mit der Spalte id und version\_id in der category Table verknüpft wird - Bzw. sollte&nbsp; ![Angry-Face](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/1X/4d894c4dd47121f4ab1fba0317660ce0f6dbd4be.png "Angry-Face")

Die refrenzierte category\_id&nbsp;existiert bereits in der category table. Sieht hier jemand den Fehler, warum ich obigen Fehler bekomme? Vielleicht sehe ich auch den Wald vor Lauter Code nicht mehr&nbsp; ![Angry-Face](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/1X/4d894c4dd47121f4ab1fba0317660ce0f6dbd4be.png "Angry-Face")

Ich verstehe es nicht, will nicht in meinen Kopf - Denn ich habe soweit ich das sehe alles richtig&nbsp; ![Angry-Face](https://europe1.discourse-cdn.com/flex013/uploads/shopware/original/1X/4d894c4dd47121f4ab1fba0317660ce0f6dbd4be.png "Angry-Face")

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)

```

Custom 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;

// new migration for the keys

ALTER TABLE `news`
                    ADD KEY `fk.news.category_id` (`category_id`,`category_version_id`),
                    ADD CONSTRAINT `fk.news.category_id` FOREIGN KEY (`category_id`,`category_version_id`) REFERENCES `category` (`id`,`version_id`) ON DELETE CASCADE ON UPDATE CASCADE

```

Hier ist die Struktur meiner news table:

[https://res.cloudinary.com/dtgdh7noz/image/upload/v1586002407/Bildschirmfoto\_2020-04-04\_um\_15.13.15\_cjrii7.png](https://res.cloudinary.com/dtgdh7noz/image/upload/v1586002407/Bildschirmfoto_2020-04-04_um_15.13.15_cjrii7.png)

Die obige referenzierte category\_id existiert bereits in der category table

[https://res.cloudinary.com/dtgdh7noz/image/upload/v1586002249/Bildschirmfoto\_2020-04-04\_um\_15.10.18\_aqobbf.png](https://res.cloudinary.com/dtgdh7noz/image/upload/v1586002249/Bildschirmfoto_2020-04-04_um_15.10.18_aqobbf.png)

---

<div class="post-metadata">

### Author: ![Shopwareianer](https://avatars.discourse-cdn.com/v4/letter/s/b4bc9f/32.png) [@Shopwareianer](https://forum.shopware.com/u/Shopwareianer)
#### Post date: [5. April 2020 um 08:44 UTC](https://forum.shopware.com/t/cannot-add-or-update-a-child-row-a-foreign-key-constraint-fails/66052/2 "2020-04-05T08:44:39Z")

</div>

UNHEX war das Problem&nbsp;
