Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine schema update always try to add NOT NULL

I have a fresh Symfony 2.8 installation, with doctrine and MySQL 5.6 stack.

After executing a doctrine:schema:update --force, i can see
Database schema updated successfully! "x" queries were executed

Here is my problem : Even if i execute it multiple time, doctrine always find schema differences.

With the --dump-sql, i can see that all of these queries are related to :

  • adding NOT NULL on string primary key
  • adding NOT NULL on datetime field

However, when i check my database, these columns already have a NOT NULL.

Here is an example on a single property/column :

class MyEntity
{
    /**
     * @ORM\Id
     * @ORM\Column(type="string", length=5, name="cd_key")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
     private $code;
     ...  

Here is the result of a SHOW CREATE TABLE my_entity; :

CREATE TABLE `my_entity` (
  `cd_key` varchar(5) COLLATE utf8_unicode_ci NOT NULL,
  `label` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `number` int(11) NOT NULL,
  PRIMARY KEY (`cd_key`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci ;

And here the query doctrine try to execute with the doctrine:schema:update command :

ALTER TABLE my_entity CHANGE cd_key cd_key VARCHAR(5) NOT NULL;
  • I clean my Symfony cache between each command execution.
  • I try to add nullable=false on @Column annotation (event if it's already defined as an @Id), but no effect.
  • a doctrine:schema:validate don't find any mapping problem (except sync of course)
  • I try to drop and recreate the full database, but no effet.

Any ideas ?

like image 234
j-guyon Avatar asked Mar 01 '17 16:03

j-guyon


1 Answers

This issue has been reported in 2017 at least here, here and here and supposed to be fixed by this PR.

Updating doctrine/dbal would be the solution (not working for me though):

$ composer require doctrine/dbal:^2.7.1

Unsetting the server version (mysql/mariadb) from the configuration would also fix the problem (still not for me though).

If one is using migrations he can still adjust them manually (but his schema will always be unsynced).

like image 72
Pierre de LESPINAY Avatar answered Nov 10 '22 01:11

Pierre de LESPINAY