使用rails迁移错误添加整数数组列

时间:2016-08-24 12:05:40

标签: ruby-on-rails postgresql ruby-on-rails-3.2 postgresql-9.4

我想添加与以下内容相同的迁移:

ALTER TABLE my_table ADD COLUMN int_array integer[];

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

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

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

感谢advence。

1 个答案:

答案 0 :(得分:0)

我找到了执行方法的解决方案。

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