DEV Community

Discussion on: Using Postgres Enum Type in Rails

Collapse
 
jasonfb profile image
Jason Fleetwood-Boldt

Unfortunately, this approach has the downside of breaking the db/schema.rb file.

I can reproduce the behavior on Rails 5.17, 5.2.2.4, and 6.0.3

if I do...

class AddXyzToUsers < ActiveRecord::Migration[5.2]
  def up
    execute <<-DDL
          CREATE TYPE xyz_setting AS ENUM (
            'apple', 'bananna', 'cherry'
          );
    DDL
    add_column :users, :xyz, :xyz_setting
  end

  def down
    remove_column  :users, :xyz
    execute "DROP type xyz_setting;"
  end
end
Enter fullscreen mode Exit fullscreen mode

then my schema file is messed up, specifically, the schema users table doesn't get output whatseover, and instead in its place is this message

Could not dump table "users" because of following StandardError

Unknown type 'xyz_setting' for column 'xyz'

I think maybe there's a gem I found that fixes this.

Collapse
 
jasonfb profile image
Jason Fleetwood-Boldt
Collapse
 
diegocasmo profile image
Diego Castillo

Did you try setting config.active_record.schema_format = :sql in the environment configuration files?

Collapse
 
jasonfb profile image
Jason Fleetwood-Boldt

Ah yes, this works as expected and leaves you with no more db/schema.rb file and instead a db/structure.sql file written in the native SQL Postgres commands instead of the Rails schema syntax.

Collapse
 
jasonfb profile image
Jason Fleetwood-Boldt