我将gender
表的user
字段用作enum
类型。
迁移也成功运行。但是schema.rb崩溃了。
运行迁移后,我的schema.rb
如下:
ActiveRecord::Schema.define(version: 2018_07_23_115046) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
# Could not dump table "users" because of following StandardError
# Unknown type 'gender' for column 'gender'
end
我的移民是:
class AddGenderToUsers < ActiveRecord::Migration[5.2]
def up
execute <<-SQL
CREATE TYPE gender AS ENUM ('male', 'female', 'not_sure', 'prefer_not_to_disclose');
SQL
add_column :users, :gender, :gender, index: true
end
def down
remove_column :users, :gender
execute <<-SQL
DROP TYPE gender;
SQL
end
end
我不明白为什么schema.rb
崩溃。
答案 0 :(得分:4)
“ Ruby样式”模式不支持Postgres自定义类型。为了使用此功能,您需要切换到SQL格式的架构。将config.active_record.schema_format
中config/application.rb
的值切换为:sql
。