【问题标题】:Using doctrine migrations when a column is to be deleted删除列时使用原则迁移
【发布时间】:2016-12-12 14:09:06
【问题描述】:

我们有一个名为enumFooType 的枚举的列类型,我们已将其添加到\Doctrine\DBal\Types\Type::addType()

在运行vendor/bin/doctrine-module migrations:diff 以生成将删除所述列的迁移时,抛出错误:

[Doctrine\DBAL\DBALException]
  Unknown column type "enumFooType" requested. Any Doctrine type that you use has to be registered with \Doctrine\DBAL\Types\Type::addType(). 
  You can get a list of all the known types with \Doctrine\DBAL\Types\Type::getTypesMap(). 
  If this error occurs during database introspection then you might have forgot to register all database types for a Doctrine Type. 
  Use AbstractPlatform#registerDoctrineTypeMapping() or have your custom types implement Type#getMappedDatabaseTypes(). 
  If the type name is empty you might have a problem with the cache or forgot some mapping information.

我猜这个错误是因为数据库有一个foo_type 标记为(DC2Type:enumFooType)

处理这些类型的删除的正确方法是什么?我的第一个想法是使用vendor/bin/doctrine-module migrations:generate 生成一个空白迁移并手动编写查询,但我想要一种更自动化的方式,如果可能的话不要手动编写任何内容。

【问题讨论】:

    标签: doctrine-orm zend-framework2


    【解决方案1】:

    TL;DR:

    DBAL 类型enumFooType 的类定义应该在运行学说命令之前存在(现在我已经写了这一行,感觉有点明显,就像“duh!”)。

    长答案:

    经过几次回滚和反复试验,我为此类操作设计了以下程序:

    1. 从实体类中删除enumFooType的属性。
    2. 创建迁移(到目前为止,EnumFooType 文件仍然存在)。
    3. 删除包含此 dbal 类型定义的 EnumFooType 类。

    之所以必须按此顺序进行是因为如果您先删除类型,则不会加载学说,因为此文件丢失,导致原始问题中发布的异常.

    此外,在您创建了迁移之后,然后删除了该类型;如果您需要回滚该更改,您必须:

    1. 恢复到上一次提交,使EnumFooType存在,并且在实体类中定义enumFooType类型的属性。
    2. 运行迁移命令进行回滚。

    【讨论】:

      猜你喜欢
      • 2018-01-30
      • 2019-01-29
      • 2011-02-19
      • 1970-01-01
      • 2019-06-10
      • 1970-01-01
      • 1970-01-01
      • 2011-11-04
      相关资源
      最近更新 更多