Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

schema.rb crashes for enum type in postgres

I am using gender field of my user table as enum type.

Migration also runs sucessfully. But the schema.rb get crashes.

After running the migration, my schema.rb looks:

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

my migration is:

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

I don't understand why the schema.rb crashes.

like image 600
Sourabh Banka Avatar asked Jul 23 '18 12:07

Sourabh Banka


1 Answers

Postgres custom types aren't supported by "Ruby-style" schemas. In order to use this functionality, you'll need to switch to a SQL-formatted schema. Switch the value of config.active_record.schema_format in config/application.rb to :sql.

like image 156
Ryan Brunner Avatar answered Sep 25 '22 13:09

Ryan Brunner