首页 > 解决方案 > 带有@ORM\Column 的 Symfony 4 Doctrine 引发 MySQL 1054 错误

问题描述

我正在向现有数据库添加新字段。为了避免一些问题,我使用了内置的 symfony 命令bin/console make:entitybin/console make:migration并且bin/console doctrine:migrations:execute <migration_id>

首先我用bin/console make:entity FirebaseToken

Your entity already exists! So let's add some new fields!

 New property name (press <return> to stop adding fields):
 > token_source

 Field type (enter ? to see all types) [string]:
 > 

 Field length [255]:
 > 45

 Can this field be null in the database (nullable) (yes/no) [no]:
 > yes

 updated: src/Entity/FirebaseToken.php

 Add another property? Enter the property name (or press <return> to stop adding fields):
 > 



  Success! 


 Next: When you're ready, create a migration with make:migration

FirebaseToken实体中生成

    /**
     * @var string
     * 
     * @ORM\Column(type="string", length=45, nullable=true)
     */
     private $token_source;

与 setter 和 getter。之后我运行in/console make:migration进行迁移,确实检查了它并且它符合预期。运行bin/console doctrine:migrations:execute <migration_id>以使用此更改更新数据库。

接下来我运行我在添加此列之前通过的测试。我使用此设置器的控制器中唯一的更改是setTokenSource() 测试失败并显示消息

"An exception occurred while executing \'INSERT INTO firebase_token (token_string, last_seen_on, token_source, user_id) VALUES (?, ?, ?, ?)\' with params ["some-long-firebase-token", "2019-05-24 08:51:53", "web", 176346]:\n\nSQLSTATE[42S22]: Column not found: 1054 Unknown column \'token_source\' in \'field list\'"

错误消息是自我解释的。我确实检查了数据库,归档在那里,按预期创建。如果我直接从 mysql 工作台运行此查询,则查询执行时不会出错。

如果我从 Entity 的新字段中删除注释@ORM\Column(type="string", length=45, nullable=true)并运行我的测试,它们会通过而没有任何错误。

我使用了一切来避免“人为错误效应”,但它不起作用。我什至尝试在我的测试环境中从头开始重新创建整个数据库,但仍然没有。为什么会这样?

使用 MySQL 5.7.26

PHP 7.1.29

Symfony 4.2

标签: mysqlsymfonydoctrine

解决方案


首先删除迁移表和目录。
之后:
1- php bin/console make:migration
2- php bin/console 学说:迁移:迁移


推荐阅读