Foreign key referencing a Uuid id with custom type with the same length #6514

Open
opened 2026-01-22 15:34:21 +01:00 by admin · 0 comments
Owner

Originally created by @ferriol on GitHub (Aug 6, 2020).

I'm defining

a Cart entity

    <entity name="Model\Cart" table="cart">
        <id name="id" type="uuid" length="36"/>
        <one-to-many field="items" target-entity="Model\CartItem"  mapped-by="cart">
            <cascade>
                <cascade-persist/>
            </cascade>
        </one-to-many>
    </entity>

and CartItem entity

    <entity name="Model\CartItem" table="cart_item">
        <id name="id" type="uuid" length="36"/>
        <many-to-one field="cart" target-entity="Model\Cart" inversed-by="items">
            <join-column name="cart_id" column-definition="" referenced-column-name="id" on-delete="CASCADE"/>
        </many-to-one>
      </entity>

Where uuid id is a Custom Type

This Doctrine translate to (I edited the real entities and sql to minimize the code)

CREATE TABLE cart (
id VARCHAR(36) CHARACTER SET utf8 NOT NULL COLLATE `utf8_unicode_ci`,
 PRIMARY KEY(id))

and

CREATE TABLE cart_item (
id VARCHAR(36) CHARACTER SET utf8 NOT NULL COLLATE `utf8_unicode_ci`,
 cart_id VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL COLLATE `utf8_unicode_ci`,
 INDEX IDX_F0FE25271AD5CDBF (cart_id),
 PRIMARY KEY(id))

I don't know why generates in CartItem: cart_id VARCHAR(255)
If in Cart is: id VARCHAR(36)

And what can I add/changein mapping to the set correct length of the cart_id FK column ?

I add the custom type for more details:

<?php

class UuidType extends StringType
{
    private const LENGTH = 36;

    public function convertToPHPValue($value, AbstractPlatform $platform)
    {
        return empty($value) ? $value : new UuidValueObject($value);
    }

    public function convertToDatabaseValue($value, AbstractPlatform $platform)
    {
        return empty($value) ? $value : (string)$value;
    }

    public function requiresSQLCommentHint(AbstractPlatform $platform)
    {
        return true;
    }

    public function getDefaultLength(AbstractPlatform $platform)
    {
        return self::LENGTH;
    }
}
Originally created by @ferriol on GitHub (Aug 6, 2020). I'm defining a Cart entity ``` <entity name="Model\Cart" table="cart"> <id name="id" type="uuid" length="36"/> <one-to-many field="items" target-entity="Model\CartItem" mapped-by="cart"> <cascade> <cascade-persist/> </cascade> </one-to-many> </entity> ``` and CartItem entity ``` <entity name="Model\CartItem" table="cart_item"> <id name="id" type="uuid" length="36"/> <many-to-one field="cart" target-entity="Model\Cart" inversed-by="items"> <join-column name="cart_id" column-definition="" referenced-column-name="id" on-delete="CASCADE"/> </many-to-one> </entity> ``` Where uuid id is a Custom Type This Doctrine translate to (I edited the real entities and sql to minimize the code) ``` CREATE TABLE cart ( id VARCHAR(36) CHARACTER SET utf8 NOT NULL COLLATE `utf8_unicode_ci`, PRIMARY KEY(id)) ``` and ``` CREATE TABLE cart_item ( id VARCHAR(36) CHARACTER SET utf8 NOT NULL COLLATE `utf8_unicode_ci`, cart_id VARCHAR(255) CHARACTER SET utf8 DEFAULT NULL COLLATE `utf8_unicode_ci`, INDEX IDX_F0FE25271AD5CDBF (cart_id), PRIMARY KEY(id)) ``` I don't know why generates in CartItem: cart_id VARCHAR(255) If in Cart is: id VARCHAR(36) And what can I add/changein mapping to the set correct length of the cart_id FK column ? I add the custom type for more details: ``` <?php class UuidType extends StringType { private const LENGTH = 36; public function convertToPHPValue($value, AbstractPlatform $platform) { return empty($value) ? $value : new UuidValueObject($value); } public function convertToDatabaseValue($value, AbstractPlatform $platform) { return empty($value) ? $value : (string)$value; } public function requiresSQLCommentHint(AbstractPlatform $platform) { return true; } public function getDefaultLength(AbstractPlatform $platform) { return self::LENGTH; } } ```
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6514