Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

db:setup fails with ActiveRecord::StatementInvalid: PG::DuplicateObject #22

Closed
emersonthis opened this issue Jun 19, 2020 · 6 comments · Fixed by #29
Closed

db:setup fails with ActiveRecord::StatementInvalid: PG::DuplicateObject #22

emersonthis opened this issue Jun 19, 2020 · 6 comments · Fixed by #29
Labels
enhancement New feature or request question Further information is requested

Comments

@emersonthis
Copy link

Describe the bug
After adding a couple migrations that use add_enum I can no longer run rake db:setup without the following error:

$ bundle exec rake db:setup
Database 'myapp_dev' already exists
Database 'myapp_test' already exists
rake aborted!
ActiveRecord::StatementInvalid: PG::DuplicateObject: ERROR:  type "question_answer_length" already exists
/Users/emerson/Code/myapp/db/schema.rb:18:in `block in <main>'
/Users/emerson/Code/myapp/db/schema.rb:13:in `<main>'
/Users/emerson/.rbenv/versions/2.6.3/bin/bundle:23:in `load'
/Users/emerson/.rbenv/versions/2.6.3/bin/bundle:23:in `<main>'

Caused by:
PG::DuplicateObject: ERROR:  type "question_answer_length" already exists
/Users/emerson/Code/myapp/db/schema.rb:18:in `block in <main>'
/Users/emerson/Code/myapp/db/schema.rb:13:in `<main>'
/Users/emerson/.rbenv/versions/2.6.3/bin/bundle:23:in `load'
/Users/emerson/.rbenv/versions/2.6.3/bin/bundle:23:in `<main>'
Tasks: TOP => db:schema:load
(See full trace by running task with --trace)

To Reproduce
Steps to reproduce the behavior:

  1. Added gem 'activerecord-postgres_enum' to Gemfile
  2. $ bundle install
  3. Create a couple migrations that use add_enum
  4. Run $ bundle exec rake db:setup

Expected behavior
Command runs without error.

Context (please complete the following information):

  • OS: MacOS
  • Version 10.15.3

Additional context
Ruby version 2.6.3p62
PostgreSQL version 12

@emersonthis
Copy link
Author

I also tried running db:reset first. The same error occurs.

@bibendi
Copy link
Owner

bibendi commented Jun 20, 2020

It doesn't look like it is happened because of this gem. Please provide a bare minimum application that is representing that bug.

@bibendi bibendi added the question Further information is requested label Jun 20, 2020
@emersonthis
Copy link
Author

emersonthis commented Jun 20, 2020 via email

@nirvdrum
Copy link
Contributor

@bibendi I'm seeing the same thing and it happens with any Rails app I've tried that uses the Ruby DSL for the schema.

The problem appears to be that create_enum doesn't have a corresponding :force option like create_table does. So, create_enum unconditionally tries to create the enum when processing schema.rb, without dropping the old enum first. If you don't drop the database after you've run your migrations and then run db:setup, the create_enum call will conflict with the existing enum.

@nirvdrum
Copy link
Contributor

Adding on, there is support in schema.rb for a :if_not_exists keyword option, like so:

create_enum :integration_account_state, [
  "connected",
  "errored",
  "pending",
], if_not_exists: true

This option isn't documented and isn't set by default, which is in contrast to how ActiveRecord handles create_table. I'd suggest that there should be a :force option as well because :if_not_exists won't handle the case where the enum variants change. If they change and you run db:setup, you'll end up with the old values.

@bibendi
Copy link
Owner

bibendi commented Aug 20, 2020

@nirvdrum, I agree with you. It looks like it needs to be implemented force: :cascade option and enable it by default in the schema.

@bibendi bibendi added the enhancement New feature or request label Aug 20, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants