8

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.

2
  • Unknown type 'gender' for column 'gender' Commented Jul 23, 2018 at 12:21
  • 1
    you should better not to create a type, but store those as an integer, then add a enum gender: { male: 0, female: 1, not_sure: 2, prefer_not_to_disclose: 3 } line to your model class. this will provide you all functionality on application layer which you expect from database. btw according to the guides declaring an enumerated gender type has zero benefit. they are stored as normal text columns. btw this might be useful Commented Jul 23, 2018 at 12:23

2 Answers 2

14

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.

Sign up to request clarification or add additional context in comments.

2 Comments

I am using rails 5.2 and after adding the line config.active_record.schema_format = :sql in config/application.rb. The problem remains. I also did the rails db:reset.
@SourabhBanka do you now have a file named db/structure.sql? Your schema should be stored in there from now on. If you do see that, you can safely delete schema.rb, it's no longer being used by Rails.
0

Custom enum types in PostgreSQL (Rails 7+)

schema.rb is crashing since Rails had no built-in support for custom enum types in PostgreSQL.

Starting from Rails 7, a new create_enum method is introduced that allows to write migrations like so:

# db/migrate/20131220144913_create_articles.rb
def up
  create_enum :article_status, ["draft", "published"]

  create_table :articles do |t|
    t.enum :status, enum_type: :article_status, default: "draft", null: false
  end
end

# There's no built in support for dropping enums, but you can do it manually.
# You should first drop any table that depends on them.
def down
  drop_table :articles

  execute <<-SQL
    DROP TYPE article_status;
  SQL
end

This way schema.rb will be updated automatically and there is no need to change config.active_record.schema_format to sql (at least for this particular case).

Sources:

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.