【发布时间】:2019-05-24 07:42:50
【问题描述】:
我正在向现有数据库添加新字段。为了避免一些问题,我使用了内置的 symfony 命令 bin/console make:entity、bin/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
【问题讨论】:
-
FirebaseToken Entity 中其他字段的结构是什么? $token_source 或 $tokenSource(CamelCase 与下划线)?
-
可能是一个非常愚蠢的问题,但测试是否使用与迁移中使用的数据库相同的数据库?
-
@Chris 我使用了数据库中的基本下划线。其他字段为 token_string、last_seen_on、user_id 和 id
-
@Jakumi 是的。测试在同一个数据库上运行。
-
你说的是同一个数据库。错误消息(除非您给了我们错误的消息)清楚地指出,您要访问一个不存在的字段。你说它确实存在。现在,我很遗憾地告诉你,这三个陈述中至少有一个不可能是真的..