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

Sou*_*nka 1 postgresql enums ruby-on-rails

我正在使用表格的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
Run Code Online (Sandbox Code Playgroud)

我的迁移是:

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
Run Code Online (Sandbox Code Playgroud)

我不明白为什么会schema.rb崩溃。

Rya*_*ner 6

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

  • @SourabhBanka 你现在有一个名为`db/structure.sql` 的文件吗?从现在开始,您的架构应该存储在那里。如果你看到了,你可以安全地删除 `schema.rb`,它不再被 Rails 使用。 (3认同)