用户迁移失败

时间:2018-11-10 13:57:59

标签: php symfony security doctrine

1)我用 make:user 命令创建了用户类:

Console image

2)我使用 make:migration 命令生成了迁移文件。

3)以下是迁移文件中的up()函数:

    final class Version20181110133851 extends AbstractMigration
    {
         public function up(Schema $schema) : void
      {

       $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 
    'mysql', 'Migration can only be executed safely on \'mysql\'.');

    $this->addSql('ALTER TABLE user ADD email VARCHAR(180) NOT NULL, ADD roles JSON NOT NULL, CHANGE id id INT AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id)');
    $this->addSql('CREATE UNIQUE INDEX UNIQ_8D93D649E7927C74 ON user (email)');
       }
    ...
    }

4)这是当我尝试使用 doctrine:migration:migrate 命令

迁移到数据库时发生的情况

Image showing the error

这是错误的内容:

  -> ALTER TABLE user ADD email VARCHAR(180) NOT NULL, ADD roles JSON NOT 
  NULL, CHANGE id id INT AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id)
  Migration 20181110133851 failed during Execution. Error An exception 
  occurred while executing 'ALTER TABLE user ADD email VARCHAR(180) NOT 
  NULL, ADD roles JSON NOT NULL, CHANGE id id INT AUTO_INCREMENT NOT NULL, 
  ADD PRIMARY KEY (id)':

  SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error 
  in your SQL syntax; check the manual that corresponds to your MariaDB 
  server version for the right syntax to use near 'JSON NOT NULL, CHANGE id 
  id INT AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id)' at line 1

  In AbstractMySQLDriver.php line 99:

  An exception occurred while executing 'ALTER TABLE user ADD email 
  VARCHAR(180) NOT NULL, ADD roles JSON NOT NULL, CHANGE id id INT 
  AUTO_INCREMENT NOT NULL, ADD PRI
  MARY KEY (id)':

 SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error 
 in your SQL syntax; check the manual that corresponds to your MariaDB 
 server version for
 the right syntax to use near 'JSON NOT NULL, CHANGE id id INT 
 AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id)' at line 1


 In PDOConnection.php line 109:

 SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error 
 in your SQL syntax; check the manual that corresponds to your MariaDB 
 server version for
  the right syntax to use near 'JSON NOT NULL, CHANGE id id INT 
 AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id)' at line 1


  In PDOConnection.php line 107:

  SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error 
 in your SQL syntax; check the manual that corresponds to your MariaDB 
  server version for
  the right syntax to use near 'JSON NOT NULL, CHANGE id id INT 
  AUTO_INCREMENT NOT NULL, ADD PRIMARY KEY (id)' at line 1

5)我已经尝试解决了几个小时,但我仍然是一个新手,我不认为自己会解决。

6)我的问题:语法错误在哪里?

2 个答案:

答案 0 :(得分:1)

Mariadb 10.1不支持本机json类型,因此您将不得不使用其他类型。更改列定义,删除此迁移,然后生成一个新的迁移。

或者,您可以升级。 Mariadb从10.2.7起开始支持json类型。 https://mariadb.com/kb/en/library/json-data-type/

答案 1 :(得分:0)

正如@ Padam87所说,MariaDB不支持本机JSON类型。如果由于某种原因无法升级数据库,但又不想更改列类型,只需在Doctrine属性中定义数据库版本,然后让Doctrine为您处理转换。

doctrine.yaml:

doctrine:
    dbal:
        server_version: '10.1.36-MariaDB'

这将在表定义中添加注释,Doctrine稍后将使用该注释对值进行编码/解码。