liquibase无法将类型枚举转换为枚举

时间:2020-04-09 15:42:08

标签: postgresql enums casting liquibase psql

我当前在数据库中通过liquibase使用以下脚本创建的枚举类型:

- changeSet:
      id: id_1
      author: my_team
      changes:
        - sql: CREATE TYPE my_team.letters AS ENUM ('A', 'B', 'C')

由于我需要在枚举中添加字母D,因此我创建了一个新的枚举

- changeSet:
      id: id_2
      author: my_team
      changes:
        - sql: CREATE TYPE my_team.letters_2 AS ENUM ('A', 'B', 'C', 'D')

然后我更新类型

  - changeSet:
      id: id_3
      author: my_team
      changes:
        - modifyDataType:
            columnName: letter
            newDataType: my_team.letters_2
            schemaName: my_team
            tableName: table_name

执行Liquibase脚本时出现以下错误

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: Invocation of init method failed; nested exception is liquibase.exception.MigrationFailedException: Migration failed for change set db/changelog/ddl-my_team-v.0.0.15.my_team::id_3::my_team team:
     Reason: liquibase.exception.DatabaseException: ERROR: cannot cast type my_team.letters to my_team.letters_2
  Position: 89 [Failed SQL: (0) ALTER TABLE my_team.table_name ALTER COLUMN case_status TYPE my_team.letters_2 USING (letter::my_team.letters_2)]

我不明白为什么,因为目标类型包括原始值的所有值。

这可以做到吗?

预先感谢

2 个答案:

答案 0 :(得分:1)

我不是Postgres专家,但是我认为以下方法可以解决问题:

  1. 将您的列letter重命名为letter_copy
  2. 创建类型为letter的新列letters_2
  3. 将所有值从letter_copy复制到letter。也许您需要从letter_copy中复制值,像update table_name set letter = letter_copy::text::letters;这样的文本。
  4. 丢弃列letter_copy

现在table_name.letter列应为letters_2枚举类型,所有值都将从枚举letters转换为枚举letters_2

答案 1 :(得分:1)

没有必要创建另一个枚举并跳过所有内容。只是alter the existing枚举:

cdfCompare