Home > front end >  doctrine altering uuid column on every migration
doctrine altering uuid column on every migration

Time:10-29

i have a question if anybody faced this kind of issue. Ive been searching for solution but i didn't find.

Every time i generate migration with doctrine, i see in migration files:

public function up(Schema $schema): void
{
  $this->addSql('ALTER TABLE verification ALTER id TYPE UUID');
  $this->addSql('ALTER TABLE verification ALTER id DROP DEFAULT');
}


public function down(Schema $schema): void
{
  $this->addSql('ALTER TABLE verification ALTER verification_id TYPE UUID');
  $this->addSql('ALTER TABLE verification ALTER verification_id DROP DEFAULT');
}

these are my annotations:

use Ramsey\Uuid\Doctrine\UuidGenerator;

/**
 * @ORM\Id
 * @ORM\Column(type="uuid", unique=true)
 * @ORM\GeneratedValue(strategy="CUSTOM")
 * @ORM\CustomIdGenerator(class=UuidGenerator::class)
 *
 * @Groups({"read"})
 **/
 private UuidInterface $id;

I use postgress image: postgres:13-alpine and "doctrine/doctrine-migrations-bundle": "^3.0",

framework "api-platform/core": "^2.6",

I've tried previous versions of doctrine-migrations-bundle along with postgres:12

Those alters are being added on every migration. I've checked my local db, drop it, remove docker volumes. Then i start containers again, run migration to be sure i got correct db schema. Problem still occurs. I don't know what cause this issue. Maybe some of you have some idea what i can look for?

CodePudding user response:

You probably have 1.7.0 version of ramsey/uuid-doctrine extension in your composer.lock, because it really has a bug with generating schema. Which was fixed a just few days ago: https://github.com/ramsey/uuid-doctrine/pull/165

So try downgrading to 1.6.0:

composer require ramsey/uuid-doctrine:1.6.0

(or upgrading to the latest dev version)

Explanation

The reason why doctrine keeps generating migration for altering the uuid column, is that the custom type class (which is Ramsey\Uuid\Doctrine\UuidType) misses the requiresSQLCommentHint flag (in 1.7.0):

    /**
     * {@inheritdoc}
     *
     * @param AbstractPlatform $platform
     *
     * @return bool
     */
    public function requiresSQLCommentHint(AbstractPlatform $platform)
    {
        return true;
    }

This flag tells doctrine to use a comment on the field to determine which custom type the values from this field need to be hydrated into. In case of Ramsey UuidType the comment should look like this: (DC2Type:uuid)

As a quick workaround, you can even just add the comment manually (without upgrading the ramsey/uuid-doctrine package):

   COMMENT ON COLUMN verification.id IS '(DC2Type:uuid)'

or via migration:

   $this->addSql('COMMENT ON COLUMN verification.id IS \'(DC2Type:uuid)\'');
  • Related