postgres中的枚举类型的schema.rb崩溃

时间:2018-07-23 12:12:53

标签: ruby-on-rails postgresql enums

我将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崩溃。

1 个答案:

答案 0 :(得分:4)

“ Ruby样式”模式不支持Postgres自定义类型。为了使用此功能,您需要切换到SQL格式的架构。将config.active_record.schema_formatconfig/application.rb的值切换为:sql