Doctrine schema update always try to add NOT NULL
Asked Answered
K

2

8

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 ?

Kalif answered 1/3, 2017 at 16:24 Comment(1)
wouldn't setting nullable=false mean that it adds NOT NULL as a part of that column? Changing it to nullable=true would allow the value to be null.Captious
M
2

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).

Miran answered 25/4, 2018 at 15:22 Comment(0)
T
-1

I've encountered a similar problem. For me deleting the table using SQL and then running again DOCTRINE:SCHEMA:UPDATE --FORCE worked for me.

It seems that doing some SQL requests manualy confuses Doctrine.

Saying that, i'm assuming you've put @ORM\Table(name="my_entity") and @ORM\Entity(repositoryClass="myrepository") over your class definition ;).

Hope it helped.

Throaty answered 1/3, 2017 at 16:35 Comment(1)
I try to delete the table (even the full database), but after a new 'doctrine:schema:update', i have the same problem. Anyway thanks for your help :)Kalif

© 2022 - 2024 — McMap. All rights reserved.