添加具有 rails 迁移错误的整数数组列

Adding integer array column with rails migration error

我想添加与以下操作相同的迁移:

ALTER TABLE my_table ADD COLUMN int_array integer[];

结果列:int_array,类型:整数[],存储:扩展

我尝试了这些选项,但没有用。

def change
  add_column :my_table, :int_array, :integer, array: true, default: [] #first option
  add_column :my_table, :int_array, :integer, array: true, default: '{}' #second options
end

发生的错误:

==  AddIntArrayToMyTable: migrating ==========================================
-- add_column(:my_table, :int_array, :integer, {:array=>true, :default=>[]})
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::InvalidTextRepresentation: ERROR: incorrect syntax for input integer: "--- []
"
: ALTER TABLE "my_table" ADD COLUMN "int_array" integer DEFAULT '--- []

==  AddIntArrayToMyTable: migrating ==========================================
-- add_column(:my_table, :int_array, :integer, {:array=>true, :default=>"{}"})
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::InvalidTextRepresentation: ERROR: incorrect syntax for input integer: "{}"

有什么想法是错误的吗? 我正在使用 rails Rails 3.2.16 和 postgreSQL 9.4

提前致谢。

我用 execute 方法找到了解决方案。

def up
  execute "ALTER TABLE my_table ADD COLUMN int_array integer[];"
end

def down
  execute "ALTER TABLE my_table DROP COLUMN int_array;"
end