gpt4 book ai didi

ruby-on-rails - 添加带有 Rails 迁移错误的整数数组列

转载 作者:行者123 更新时间:2023-11-29 13:52:06 25 4
gpt4 key购买 nike

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

ALTER TABLE my_table ADD COLUMN int_array integer[];

结果列:int_array,类型:integer[],存储: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

提前致谢。

最佳答案

我用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

关于ruby-on-rails - 添加带有 Rails 迁移错误的整数数组列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39122852/

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