liquibase 无法将类型枚举转换为枚举
liquibase cannot cast type enum to enum
我目前在我的数据库中使用以下脚本通过 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 专家,但我认为以下内容可以解决问题:
- 将您的列
letter
重命名为 letter_copy
。
- 创建类型为
letters_2
的新列 letter
。
- 将所有值从
letter_copy
复制到 letter
。也许您必须将 letter_copy
中的值复制为 update table_name set letter = letter_copy::text::letters;
. 之类的文本
- 删除列
letter_copy
。
现在 table_name.letter
列应该有一个 letters_2
枚举类型,所有值都从枚举 letters
转换为枚举 letters_2
.
无需创建另一个枚举并跳过箍以将所有内容理顺。只是 alter the existing 枚举:
ALTER letter ADD VALUE 'D' after 'C';
我目前在我的数据库中使用以下脚本通过 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 专家,但我认为以下内容可以解决问题:
- 将您的列
letter
重命名为letter_copy
。 - 创建类型为
letters_2
的新列letter
。 - 将所有值从
letter_copy
复制到letter
。也许您必须将letter_copy
中的值复制为update table_name set letter = letter_copy::text::letters;
. 之类的文本
- 删除列
letter_copy
。
现在 table_name.letter
列应该有一个 letters_2
枚举类型,所有值都从枚举 letters
转换为枚举 letters_2
.
无需创建另一个枚举并跳过箍以将所有内容理顺。只是 alter the existing 枚举:
ALTER letter ADD VALUE 'D' after 'C';