【问题标题】:Symfony 4 Doctrine with @ORM\Column throws MySQL 1054 error带有@ORM\Column 的 Symfony 4 Doctrine 引发 MySQL 1054 错误
【发布时间】:2019-05-24 07:42:50
【问题描述】:

我正在向现有数据库添加新字段。为了避免一些问题,我使用了内置的 symfony 命令 bin/console make:entitybin/console make:migrationbin/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 &lt;migration_id&gt; 以使用此更改更新数据库。

接下来我运行我的测试,这些测试在添加此列之前通过。唯一的变化是我使用这个设置器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 是的。测试在同一个数据库上运行。
  • 你说的是同一个数据库。错误消息(除非您给了我们错误的消息)清楚地指出,您要访问一个不存在的字段。你说它确实存在。现在,我很遗憾地告诉你,这三个陈述中至少有一个不可能是真的..

标签: mysql symfony doctrine


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2011-03-16
    • 1970-01-01
    • 2019-01-12
    • 2022-08-08
    • 2019-03-28
    • 2020-10-13
    • 1970-01-01
    • 2017-04-07
    • 2016-02-07
    相关资源
    最近更新 更多