# "Cannot add foreign key constraint" bei Verweis auf eigenes Repository

**URL:** <https://forum.shopware.com/t/cannot-add-foreign-key-constraint-bei-verweis-auf-eigenes-repository/65489>\
**Category:** Programmierung\
**Created:** [13. März 2020 um 11:43 UTC](https://forum.shopware.com/t/cannot-add-foreign-key-constraint-bei-verweis-auf-eigenes-repository/65489 "2020-03-13T11:43:29Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![compragmbh](https://avatars.discourse-cdn.com/v4/letter/c/d07c76/32.png) [@compragmbh](https://forum.shopware.com/u/compragmbh)\
**Post date:** [13. März 2020 um 11:43 UTC](https://forum.shopware.com/t/cannot-add-foreign-key-constraint-bei-verweis-auf-eigenes-repository/65489/1 "2020-03-13T11:43:29Z")

</div>

Hallo Zusammen,

Ich habe über ein Plugin mehrere **eigene Repositories** angelegt inkl. Entity, Definition und Collection Klasse.

Eines der Repositories (EsiMapping) soll nun per FkField auf ein zweites durch mich angelegtes Repository (EsiAttributeValues) referenzieren.

**Beim Installieren kriege ich nun einen „General error: 1215 Cannot add foreign key constraint“.**

&nbsp;

**EsiAttributeValuesDefinition.php** (Das Repository auf das referenziert werden soll):

```
protected function defineFields(): FieldCollection
{
    return new FieldCollection([
        (new IdField('id', 'id'))->addFlags(new PrimaryKey(), new Required()),
        (new IntField('configuration_id', 'configurationId'))->addFlags(new Required()),
        (new IntField('eevo_id', 'eevoId'))->addFlags(new Required()),
        (new StringField('eevo_label', 'eevoLabel'))->addFlags(new Required())
    ]);
}

```

&nbsp;

**EsiMappingDefinition.php** (Das Repository, welches auf EsiAttributeValues per FkField verweisen soll):

```
protected function defineFields(): FieldCollection
{
    return new FieldCollection([
        (new IdField('id', 'id'))->addFlags(new PrimaryKey(), new Required()),
        (new IntField('configuration_id', 'configurationId'))->addFlags(new Required()),
        (new FkField('eevo_id', 'eevoId', EsiAttributeValuesDefinition::class))->addFlags(new Required()),
        (new StringField('eevo_label', 'eevoLabel'))->addFlags(new Required()),
        (new IdField('shop_id', 'shopId'))->addFlags(new Required()),
        (new StringField('shop_label', 'shopLabel'))->addFlags(new Required()),
        new IntField('position', 'position'),

        new ManyToOneAssociationField('eevo', 'eevo_id', EsiAttributeValuesDefinition::class)
    ]);
}

```

&nbsp;

Meine SQL Statements, um die Tabellen in de Migrations zu erzeugen sehen wie folgt aus:

**MigrationEsiAttributeValues** :

```
CREATE TABLE IF NOT EXISTS `esi_attribute_values` (
    `id` BINARY(16) NOT NULL,
    `configuration_id` INT(11) NOT NULL,
    `eevo_id` INT(11) NOT NULL,
    `eevo_label` VARCHAR(255) 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;

```

&nbsp;

**MigrationEsiMapping** :

```
CREATE TABLE IF NOT EXISTS `esi_mapping` (
    `id` BINARY(16) NOT NULL,
    `configuration_id` INT(11) NOT NULL,
    `eevo_id` BINARY(16) NOT NULL,
    `eevo_label` VARCHAR(255) NOT NULL,
    `shop_id` BINARY(16) NULL,
    `shop_label` VARCHAR(255) NOT NULL,
    `position` INT(11) NULL,
    `created_at` DATETIME(3) NOT NULL,
    `updated_at` DATETIME(3) NULL,
    PRIMARY KEY (`id`),
    KEY `fk.esi_mapping.eevo_id` (`eevo_id`),
    CONSTRAINT `fk.esi_mapping.eevo_id` FOREIGN KEY (`eevo_id`) REFERENCES `esi_attribute_values` (`id`) ON DELETE SET NULL ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

```

**Hat jemand eine Lösung?** Ein Foreign Key auf die Shopware-eigene UserDefinition funktioniert problemlos.

&nbsp;

Viele Grüße  
Dennis

---

<div class="post-metadata">

**Author:** ![mealy993](https://avatars.discourse-cdn.com/v4/letter/m/e9a140/32.png) [@mealy993](https://forum.shopware.com/u/mealy993)\
**Post date:** [23. Januar 2021 um 12:58 UTC](https://forum.shopware.com/t/cannot-add-foreign-key-constraint-bei-verweis-auf-eigenes-repository/65489/2 "2021-01-23T12:58:31Z")

</div>

Folgendes hat bei mir geholfen als ich das Problem hatte.

(Am Beispiel von @compragmbh‍)

Das Plugin vorerst ohne die zweite Migration " **MigrationEsiMapping**" installieren und danach die zweite Migration " **MigrationEsiMapping**" hochladen und diese per plugin:reinstall oder&nbsp;

```
/bin/console database:migrate DeinPluginName --all

```

triggern.

Falls der Fehler&nbsp; **Column already exists** &nbsp;auftritt dann die bereits durchgeführten Migrations aus dem Migration-Ordner vom Server löschen und nur mit den anderen Migrations versuchen.

Viele Grüße  
Markus
