数据库使用的是 postgreSQL 在按着 carrierwave 给出的官方文档操作时出现了问题
rails g migration add_avatars_to_users avatars:array #我写的是add_subimages_to_images subimages:array ,改成:json也试过,不行
rake db:migrate #上一部并没有出问题,但是这步出问题了
以下是又长又多的 log,我稍微删减了一下:
== 20150502083130 AddSubimagesToImages: migrating =============================
-- add_column(:images, :subimage, :array)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:
PG::SyntaxError: ERROR: syntax error at or near "array"
LINE 1: ALTER TABLE "images" ADD COLUMN "subimage" array
^
.........
ActiveRecord::StatementInvalid: PG::SyntaxError: ERROR: syntax error at or near "array"
LINE 1: ALTER TABLE "images" ADD COLUMN "subimage" array
^
: ALTER TABLE "images" ADD COLUMN "subimage" array
...........
PG::SyntaxError: ERROR: syntax error at or near "array"
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
是不是 pg 不支持那种方法呢?可是我又不能换数据库呀,做了好久的项目.......
不知道为什么,rails 自己生成的 migration 代码有错误 应该改成这样:
class AddSubimagesToImages < ActiveRecord::Migration
def change
add_column :images, :subimages, :string, array: true, default: '{}'
end
end
即可