2

I'd like to address question to those who use Mysql + Doctrine ORM + Doctrine migrations I have an association:

    /**
     * @var User
     *
     * @ORM\ManyToOne(targetEntity="User")
     * @ORM\JoinColumn(nullable=true)
     */
    protected $sender;

(key point here is nullable=true. Some attributes are left behind intentionally)

  1. i do migraions:diff - generated migration with a piece sender_id VARCHAR(255) DEFAULT NULL
  2. run migrate
  3. then i do diff again, expecting no new migrations generated
  4. but no, i see new file with line CHANGE sender_id sender_id VARCHAR(255) DEFAULT NULL

It means that nothing is going to be changed, however doctrine dbal under Mysql driver doesn't see from entity annotations that DEFAULT NULL

I tried to add @ORM\Column(options={"default": NULL}).

That helped with preventing DEFAULT NULL in migrations, but FK and Index were dropped in this case.

Tried also add columnDefinition="VARCHAR(255) DEFAULT NULL" but also didn't work

How did you solve this issue?

Shadow
  • 33,525
  • 10
  • 51
  • 64
D.Samchuk
  • 1,219
  • 9
  • 9

1 Answers1

1

I had the same issue. In my case (Symfony, using Docker with the image "mariadb:10.5.8"), the solution was to set the correct server_version in the config:

doctrine:
    dbal:
        server_version: '5.7' # (this was previously incorrect in my setting!)

to

        server_version: 'mariadb-10.5.8' # (correct version for my setting, please adjust accordingly!)

Afer doing that and restarting Docker, the diff was empty ("No changes detected in your mapping information.").

All credit goes to the post of "tristanbes commented on Apr 4, 2018", see the following link:

https://github.com/doctrine/dbal/issues/2985

g35232t54
  • 37
  • 1
  • 7
  • It's important to note that the 'server_version' parameter can be overwritten by DATABASE_URL variable (.env or .env.load config file). You must change the 'serverVersion' parameter if exists. DATABASE_URL=mysql://user:password@127.0.0.1:3306/database?serverVersion=mariadb-10.5.11 – lenko Aug 24 '21 at 08:45
  • Typo error in my comment: .env or .env.local – lenko Aug 24 '21 at 08:51
  • Sadly, this doesn't make any difference when using DoctrineMigrationBundle, it keeps regenerating useless sql statements such as enum changes, dropping foreign key just to recreate them, etc... – Herz3h Sep 15 '21 at 07:51