j-guyon
j-guyon

Reputation: 1832

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 :

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;

Any ideas ?

Upvotes: 8

Views: 2990

Answers (2)

Pierre de LESPINAY
Pierre de LESPINAY

Reputation: 46158

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

Upvotes: 2

Edouard
Edouard

Reputation: 389

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.

Upvotes: -1

Related Questions