Adding a Postgres ENum type via. migration breaks db/schema.rb table entry

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Adding a Postgres ENum type via. migration breaks db/schema.rb table entry

alexander.maznev
Hello, So I'm adding a Postgres Enum Type with a migration but I'm having a problem with db/schema.rb output breaking when I do this:

def up
  execute
<<-SQL.squish
     CREATE TYPE three_letters_type AS ENUM
(
       
'A',
       
'B',
       
'C',
       
);
  SQL
  execute
<<-SQL.squish
     ALTER TABLE test
     ADD COLUMN three_letters three_letters_type
;
  SQL
 
end


I end up with the following comments in db/schema.rb -


# Could not dump table "test" because of following StandardError
#   Unknown type 'three_letters_type' for column 'three_letters'

It's quite important for me to be able to use Enums because unique-indexes across several columns are expensive and it's highly preferable not to use t.string (varchar) here. Would be great to hear if there is a work-around for this, thanks.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/67983333-042c-4437-8fc5-edeb06fa0300%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding a Postgres ENum type via. migration breaks db/schema.rb table entry

botp
On Wed, Nov 11, 2015 at 4:33 PM,  <[hidden email]> wrote:
> # Could not dump table "test" because of following StandardError
> #   Unknown type 'three_letters_type' for column 'three_letters'

activerecord will complain : )  see
http://edgeguides.rubyonrails.org/active_record_postgresql.html


so to dump schema,  try

config.active_record.schema_format = :sql    #instead of :ruby default

kind regards
--botp

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/CAAwHHQgDW5ZXNxGrEqw5XF0wJJMkrv9KFTuHVDTPoOLquUr6ug%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Rio
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Adding a Postgres ENum type via. migration breaks db/schema.rb table entry

Rio
In reply to this post by alexander.maznev
I found another way around this so you could still use the ruby version of schema dumper.  You must create an initializer to add your custom types to the active record psotgress adapter.  I wrote up the answer here:

https://stackoverflow.com/a/45430348/1019660

(This post was the first place I saw talking about it so wanted to make sure anyone else stumbling across it found a good answer)

On Wednesday, November 11, 2015 at 10:59:55 AM UTC+1, [hidden email] wrote:
Hello, So I'm adding a Postgres Enum Type with a migration but I'm having a problem with db/schema.rb output breaking when I do this:

def up
  execute
<<-SQL.squish
     CREATE TYPE three_letters_type AS ENUM
(
       
'A',
       
'B',
       
'C',
       
);
  SQL
  execute
<<-SQL.squish
     ALTER TABLE test
     ADD COLUMN three_letters three_letters_type
;
  SQL
 
end


I end up with the following comments in db/schema.rb -


# Could not dump table "test" because of following StandardError
#   Unknown type 'three_letters_type' for column 'three_letters'

It's quite important for me to be able to use Enums because unique-indexes across several columns are expensive and it's highly preferable not to use t.string (varchar) here. Would be great to hear if there is a work-around for this, thanks.

--
You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [hidden email].
To post to this group, send email to [hidden email].
To view this discussion on the web visit https://groups.google.com/d/msgid/rubyonrails-talk/ea21716f-0142-47f8-b1fe-b330e87c8370%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Loading...