DoctrineMigrationsBundle icon indicating copy to clipboard operation
DoctrineMigrationsBundle copied to clipboard

Generated columns added to migrations file every time - any way to tell the migration to ignore single fields?

Open gravitiq-cm opened this issue 2 years ago • 5 comments

I have a generated column in my entity

/**
 * @ORM\Column(type="boolean", columnDefinition="TINYINT(1) GENERATED ALWAYS AS (xyz) STORED", insertable=false, updatable=false)
 * @ORM\Ignore()
 */

This works fine. However, every time I run make:migration (even with no new changes) a new migration file is created with the details of the generated columns. E.g. something like this:

        $this->addSql('ALTER TABLE my_table ADD my_field TINYINT(1) GENERATED ALWAYS AS (xyz) STORED');

I guess this is because the migration diff cannot see details of the generated column?

Is there any way to a) see/debug what the migration is comparing to what (to see if I just need to tweak the text in the columnDefinition) b) to mark the generated column as "ignore during migration" so make:migration does not consider it when creating migrations ?

gravitiq-cm avatar Mar 01 '23 10:03 gravitiq-cm

Update: I tried to do some debugging in Doctrine\DBAL\Schema\Comparator but it seems there's no data about the generated field pulled from the database.

In the short term, is there a way to mark this column as excluded from consideration when calculating the migration diff?

gravitiq-cm avatar Mar 01 '23 12:03 gravitiq-cm

Is there any update about this issue?

armellin avatar Aug 10 '23 18:08 armellin

Same problem. I've gone as far as to mimic the complete create statement produced by MySQL:

columnDefinition: "tinyint GENERATED ALWAYS AS (_utf8mb4'ROLE_ADMIN' member of (`Roles`)) VIRTUAL"

But no matter what I do I always end up with a migration.

// this up() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE Users CHANGE isAdmin isAdmin tinyint GENERATED ALWAYS AS (_utf8mb4\'ROLE_ADMIN\' member of (`Roles`)) VIRTUAL');

// this down() migration is auto-generated, please modify it to your needs
$this->addSql('ALTER TABLE Users CHANGE isAdmin isAdmin TINYINT(1) DEFAULT NULL');

whataboutpereira avatar Jul 25 '24 14:07 whataboutpereira

You have to either add NOT NULL to your generated column definition or add nullable: true to the column definition in your entity. This would help at least pass schema validation check BUT there's definetely a bug that Doctrine just doesn't compare column definition from attributes with actual one from DB. If you change columnDefinition param without changing other attributes of entity, migration won't be generated

HEKET313 avatar Aug 12 '24 12:08 HEKET313

You have to either add NOT NULL to your generated column definition or add nullable: true to the column definition in your entity. This would help at least pass schema validation check BUT there's definetely a bug that Doctrine just doesn't compare column definition from attributes with actual one from DB. If you change columnDefinition param without changing other attributes of entity, migration won't be generated

Thank you! MySQL didn't seem to accept NOT NULL on generated columns, however nullable: true worked.

whataboutpereira avatar Aug 14 '24 07:08 whataboutpereira