Symfony. Migrando de ids a uuids en Doctrine

Descubre cómo migrar de ids autoincrementales generados por la base de datos a uuids

Instalación de ramsey/uuid-doctrine

composer require ramsey/uuid-doctrine
use Ramsey\Uuid\Uuid;

$uuid = Uuid::uuid4();

Migrando nuestras entidades a uuid

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*/
class SomeEntity
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
use Doctrine\ORM\Mapping as ORM;
use Ramsey\Uuid\Doctrine\UuidGenerator;

/**
* @ORM\Entity
*/
class SomeEntity
{
/**
* @var \Ramsey\Uuid\UuidInterface
*
* @ORM\Id
* @ORM\Column(type="uuid", unique=true)
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)
*/

private $id;

Preparando la migración

bin/console make:migration
ALTER TABLE my_entity CHANGE id id CHAR(36) NOT NULL COMMENT \'(DC2Type::uuid)\''ALTER TABLE my_entity_category CHANGE my_entity_id my_entity_id CHAR(36) NOT NULL COMMENT \'(DC2Type::uuid)\'';

General error referencing column and referenced column in a foreign key constraint are incompatible

¿Qué habría que hacer?

Id to uuid

composer require habbim/id-to-uuid
<?php

namespace Application\Migrations;

use Doctrine\DBAL\Schema\Schema;
use Habbim\IdToUuid\IdToUuidMigration;

class VersionXYZ extends IdToUuidMigration
{
public function postUp(Schema $schema): void
{
$this->migrate('my_entity');
}
}
bin/console doctrine:migrations:migrate
* @ORM\GeneratedValue(strategy="CUSTOM")
* @ORM\CustomIdGenerator(class=UuidGenerator::class)

Conclusiones

¿Quieres recibir más artículos como este?

Entre paseo y paseo con Simba desarrollo en Symfony y React

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store