# Missing version reference for foreign key column product.id for definition association my\_entity.product

**URL:** <https://forum.shopware.com/t/missing-version-reference-for-foreign-key-column-product-id-for-definition-association-my-entity-product/89784>\
**Category:** Programmierung\
**Created:** [16. August 2021 um 11:38 UTC](https://forum.shopware.com/t/missing-version-reference-for-foreign-key-column-product-id-for-definition-association-my-entity-product/89784 "2021-08-16T11:38:43Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![lucamario](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/lucamario/32/15269_2.png) [@lucamario](https://forum.shopware.com/u/lucamario)\
**Post date:** [16. August 2021 um 11:38 UTC](https://forum.shopware.com/t/missing-version-reference-for-foreign-key-column-product-id-for-definition-association-my-entity-product/89784/1 "2021-08-16T11:38:43Z")

</div>

Hallo zusammen,

ich habe eine OneToOneAssociation meiner Entity zu `product` erstellt.

Das FkField befindet sich bei meiner Entity. Soweit funktioniert die Association auch einwandfrei. Jedoch bekomme ich mit dal:validate den Fehler:

Missing version reference for foreign key column product.id for definition association my\_entity.product

Was fehlt hier noch auf Seiten meiner Entity? Ich werde aus der product version nicht schlau.

Viele Grüße

Luca

---

<div class="post-metadata">

**Author:** ![AlexGalax](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/alexgalax/32/9925_2.png) [@AlexGalax](https://forum.shopware.com/u/AlexGalax)\
**Post date:** [16. August 2021 um 11:50 UTC](https://forum.shopware.com/t/missing-version-reference-for-foreign-key-column-product-id-for-definition-association-my-entity-product/89784/2 "2021-08-16T11:50:46Z")

</div>

Für eine Association müssen alle primary keys angegeben werden, welche in `products ` eine Kombination aus `id` und `version_id` ist.

---

<div class="post-metadata">

**Author:** ![lucamario](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/lucamario/32/15269_2.png) [@lucamario](https://forum.shopware.com/u/lucamario)\
**Post date:** [17. August 2021 um 10:38 UTC](https://forum.shopware.com/t/missing-version-reference-for-foreign-key-column-product-id-for-definition-association-my-entity-product/89784/3 "2021-08-17T10:38:28Z")

</div>

Moin Alex,

Hier meine Migration:

```auto
$connection->executeStatement("
            CREATE TABLE IF NOT EXISTS `my_entity` (
          `id` BINARY(16) NOT NULL,
          `product_id` BINARY(16) NOT NULL,
          `css_class` VARCHAR(255) NULL,
          `created_at` DATETIME(3) NOT NULL,
          `updated_at` DATETIME(3) NULL,
            PRIMARY KEY (`id`, `product_id`),
          CONSTRAINT `fk.my_entity.product_id` FOREIGN KEY (`product_id`)
            REFERENCES `product` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
            ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;
        ");

```

Wie würde die Migration demnach aussehen? `product_id` ist schon drin. Aber `version_id` würde ja eine neue Spalte bedeuten. Einen weiteren foreign key aus `product` lässt er mich aber nicht hinzufügen.

LG Luca

---

<div class="post-metadata">

**Author:** ![AlexGalax](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/alexgalax/32/9925_2.png) [@AlexGalax](https://forum.shopware.com/u/AlexGalax)\
**Post date:** [17. August 2021 um 11:13 UTC](https://forum.shopware.com/t/missing-version-reference-for-foreign-key-column-product-id-for-definition-association-my-entity-product/89784/4 "2021-08-17T11:13:47Z")

</div>

> [@lucamario](#):
>
> Einen weiteren foreign key aus `product` lässt er mich aber nicht hinzufügen.

wieso nicht?

```sql
CREATE TABLE IF NOT EXISTS `my_entity` (
    `id` BINARY(16) NOT NULL,
    `product_id` BINARY(16) NOT NULL,
    `product_version_id` BINARY(16) NOT NULL,
    `css_class` VARCHAR(255) NULL,
    `created_at` DATETIME(3) NOT NULL,
    `updated_at` DATETIME(3) NULL,
    PRIMARY KEY (`id`, `product_id`, `product_version_id`),
    CONSTRAINT `fk.my_entity.product_id__product_version_id` FOREIGN KEY (`product_id`, `product_version_id`)
        REFERENCES `product` (`id`, `version_id`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

```

---

<div class="post-metadata">

**Author:** ![lucamario](https://dub1.discourse-cdn.com/flex013/user_avatar/forum.shopware.com/lucamario/32/15269_2.png) [@lucamario](https://forum.shopware.com/u/lucamario)\
**Post date:** [17. August 2021 um 12:10 UTC](https://forum.shopware.com/t/missing-version-reference-for-foreign-key-column-product-id-for-definition-association-my-entity-product/89784/5 "2021-08-17T12:10:06Z")

</div>

Hat geklappt, ich hatte einen Tippfehler in der CONSTRAINT.

Ich danke dir vielmals!

Zur Vollständigkeit hier noch meine EntityDefinition, die den Fehler mit einem ReferenceVersionField behebt:

```auto
return new FieldCollection([
            (new IdField("id", "id"))->addFlags(new ApiAware(), new PrimaryKey(), new Required()),
            (new ReferenceVersionField(ProductDefinition::class, "product_version_id"))->addFlags(new ApiAware(), new Required()),
            (new FkField("product_id", "productId", ProductDefinition::class))->addFlags(new ApiAware(), new Required()),
            (new StringField("css_class", "cssClass"))->addFlags(new ApiAware()),
            (new OneToOneAssociationField("product", "product_id", "id", ProductDefinition::class, false))->addFlags(new ApiAware()),
        ]);

```
