gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 10:14:45 25 4
gpt4 key购买 nike

我目前在我的数据库中使用以下脚本通过 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)]

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

有什么方法可以做到这一点?

提前致谢

最佳答案

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

  1. 将您的列 letter 重命名为 letter_copy
  2. 创建letters_2 类型的新列letter
  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

关于postgresql - liquibase 无法将类型枚举转换为枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61124906/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com