我需要帮助来完成使用 rails g migration
命令将数组列添加到资源的步骤。我有一个 postgresql 数据库。我需要制作一个字符串数组和另一个整数数组。我想要它以便在我的 schema.rb
文件中有...
create_table "streams", force: true do |t|
t.array "ids" #strings
t.array "lengths" #integers
最佳答案
您必须创建新的迁移,以便 rails g migration change_column_type_of_ids_and_length
。然后编辑生成的迁移文件。
首先尝试使用
change_column
方法。如果这有效,您的数据将被保留。否则,尝试第 2 步change_column :streams , :ids , :string , array: true , default: [] change_column :streams , :lengths, :integer ,array: true , default: []
这里我们要删除数据列,然后创建新数据。
remove_column :streams, :ids remove_column :streams, :lengths add_column :streams , :ids , :string ,array: true , default: [] add_column :streams , :lengths , :integer ,array: true , default: []
关于ruby-on-rails - 向资源添加数组列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23913128/