我在提供者模型(Provider Model)中有一个benefit_type整数列,它是一个枚举(enum)列。
Provider.rb
enum: ['abc', 'bcd']
现在我想迁移到array_enum
Provider.rb
array_enum: {'abc': 0, 'bcd': 1}
因此,为了适应这种变化,我想将我的列更改为整数数组。在我的迁移中,我有以下内容:
change_column :providers, :benefit_type, :integer, array: true, default: {}, using: "(string_to_array(benefit_type, ','))"
错误:
Caused by:
PG::UndefinedFunction: ERROR: function string_to_array(integer, unknown) does not exist
LINE 1: ...ALTER COLUMN "benefit_type" TYPE integer[] USING (string_to_...
^
HINT: No function matches the given name and argument types. You might need to add explicit
type casts.
也尝试过:
change_column :providers, :benefit_type, :integer, array: true, default: []
错误:
Caused by:
ActiveRecord::StatementInvalid: PG::DatatypeMismatch: ERROR: column "benefit_type" cannot
be cast automatically to type integer[]
HINT: You might need to specify "USING benefit_type::integer[]".
: ALTER TABLE "providers" ALTER COLUMN "benefit_type" TYPE integer[], ALTER COLUMN
"benefit_type" SET DEFAULT '{}'
using: 'ARRAY[benefit_type]::INTEGER[]'
。也许这可以解决你的问题。 - Pravesh Khatrichange_column :providers, :benefit_type, :integer, array: true, default: [], using: 'ARRAY[benefit_type]::INTEGER[]'
。 - Pravesh Khatri