symfony - Doctrine 对 ENUM 类型的实体做出了不正确的差异
问题描述
我通过cookbook为实体CommandENUM
的属性类型做了一个特殊类型。
该属性如下所示:
/**
* @var CommandType
*
* @ORM\Column(type="command_type")
*/
protected $type;
这个片段描述了新的学说类型:
final class CommandTypeType extends EnumerableType
{
public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
{
return 'VARCHAR(30)';
}
public function getName(): string
{
return 'command_type';
}
protected function getClassName(): string
{
return CommandType::class;
}
}
/bin/console doctrine:migrations:diff
在第一次为实体运行命令后,我得到了迁移,它看起来是正确的
final class Version2020 072720500 extends AbstractMigration
{
public function up(Schema $schema) : void
{
$this->addSql("CREATE TABLE commands (
id INT AUTO_INCREMENT NOT NULL,
name VARCHAR(30) DEFAULT NULL,
command VARCHAR(100) DEFAULT NULL,
type varchar(30) NOT NULL,
PRIMARY KEY(id)
) DEFAULT CHARACTER SET UTF8 COLLATE `UTF8_unicode_ci` ENGINE = InnoDB");
}
public function down(Schema $schema) : void
{
$this->addSql('DROP TABLE commands');
}
}
下一步是运行命令/bin/console doctrine:migrations:migrate
,它工作正常,表已创建。
然后我/bin/console doctrine:migrations:diff
再次运行命令并获得新的迁移
final class Version20200727205035 extends AbstractMigration
{
public function up(Schema $schema) : void
{
$this->addSql('ALTER TABLE commands CHANGE type type VARCHAR(30) NOT NULL');
}
public function down(Schema $schema) : void
{
$this->addSql('ALTER TABLE commands CHANGE type type VARCHAR(30) CHARACTER SET utf8 NOT NULL COLLATE `utf8_unicode_ci`');
}
}
我不知道为什么第二个差异会产生这种奇怪的迁移。
我做错了什么?
- Symfony 4.4
- 学说/学说迁移捆绑包 3.0.1
- 学说/迁移 3.0.1
解决方案
Doctrine 需要在列中添加注释以检测自定义类型已应用于列。
检查requiresSQLCommentHint
方法是否已实现并返回 true 或将其添加到您的自定义类型。
final class CommandTypeType extends EnumerableType
{
...
public function requiresSQLCommentHint(AbstractPlatform $platform): bool
{
return true;
}
}
您应该会在下次迁移时看到将添加到您的列中的评论,仅此而已。
推荐阅读
- .net - Windows 10 上的 SSH.NET 如何在我们的远程 Linux 机器上启动一个保持运行的进程?
- ibm-mobilefirst - MobileFirst 加密/解密用户凭证
- c - 删除结束行字符
- reactjs - 无法从表单接收数据
- apache-spark - Apache/Cloudera HUE / Livy Spark Server - InterpreterError: 无法启动解释器
- git - 通过特定提交将 git 更改从一个分支隔离到另一个分支
- optimization - 基于变量在阶跃函数中生成值
- node.js - 为什么 MongoDB 为该模式中不存在的字段创建索引?
- python - 从 Pandas DataFrame 创建 Spark DataFrame
- azure-language-understanding - 无法映射到 personName 预建实体