Home > OS >  Shopware 6 - Composite primary key with columns of multiple types, in entity definition
Shopware 6 - Composite primary key with columns of multiple types, in entity definition

Time:03-05

Here is the problem description:

I created a custom entity and a definition for it. The defineFields function body is this:

protected function defineFields(): FieldCollection {
    return new FieldCollection([
        (new FkField('product_family_id', 'productFamilyId', ProductFamilyDefinition::class, 'id'))->addFlags(new ApiAware(), new Required(), new PrimaryKey()),
        (new StringField('area_id', 'areaId'))->addFlags(new ApiAware(), new Required(), new PrimaryKey()),
        (new StringField('block_number', 'blockNumber'))->addFlags(new ApiAware(), new Required(), new PrimaryKey()),
        (new StringField('text', 'text')),
        new CreatedAtField(),
        new UpdatedAtField(),
        (new ManyToOneAssociationField('productFamily', 'product_family_id', ProductFamilyDefinition::class, 'id')),
    ]);
}

So, as you can see, we need to have a composite primary key of product_family_id, area_id and block_number. The thing is, the fields area_id and block_number need to be of type string. But because the PrimaryKey() flags were added to those fields, Shopware doesn't want to perform insert operations because shopware now wants the two fields to have UUID length (16 characters). If we remove the PrimaryKey() flag then area_id and block_number fields can have whichever size, as they should.

An example of a request / response operation for this problem is to be found here:

Example request:

URL: http://localhost/api/_action/sync
BODY: [{"entity":"product_family_additional_info","action":"upsert","payload":[{"productFamilyId":"cb5e21bda1d8488ab5dda9d3fb4e4fe4","areaId":"22","blockNumber":"33","text":"text121"}]}]

Example response:

{
    "success": false,
    "data": [
        {
            "result": [
                {
                    "entities": [],
                    "errors": [
                        {
                            "status": "400",
    

                    "code": "FRAMEWORK__UUID_INVALID_LENGTH",
                        "title": "Bad Request",
                        "detail": "UUID has a invalid length. 16 bytes expected, 2 given. Hexadecimal reprensentation: 3232",
                        "meta": {
                            "parameters": {
                                "length": 2,
                                "hex": "3232"
                            }
                        }
                    }
                ]
            }
        ],
        "extensions": []
    }
],
"deleted": [],
"notFound": [],
"extensions": []
}

So, is there way to have a composite primary key with columns of multiple types? And when we add the PrimaryKey() flag, the column not to be forced to be of UUID length.

CodePudding user response:

Shopware expects that PrimaryKey fields are Uuids, but in your case you could add a own IdField to your definition and in the Database migration that creates the table you can add a unique constraint for the 3 fields you currently want to use as a composite primary key.

  • Related