6

Rails rookie attempting to migrate DB to heroku. Excuse the juiciness of this error message but I figure it may be important to include it all. I have no idea why this is happening or what it means. I have only one products table in my schema and one create products migration.

Migrating to CreateProducts (20140804010848)
== 20140804010848 CreateProducts: migrating ===================================
-- create_table(:products)
PG::DuplicateTable: ERROR:  relation "products" already exists
: CREATE TABLE "products" ("id" serial primary key, "name" character varying(255), "price" float, "description" text, "image" character varying(255), "color" character varying(255), "created_at" timestamp, "updated_at" timestamp) 
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

PG::DuplicateTable: ERROR:  relation "products" already exists
: CREATE TABLE "products" ("id" serial primary key, "name" character varying(255), "price" float, "description" text, "image" character varying(255), "color" character varying(255), "created_at" timestamp, "updated_at" timestamp) /app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `async_exec'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:128:in `block in execute'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:373:in `block in log'
/app/vendor/bundle/ruby/2.0.0/gems/activesupport-4.1.4/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract_adapter.rb:367:in `log'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/postgresql/database_statements.rb:127:in `execute'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract/schema_statements.rb:205:in `create_table'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/migration.rb:649:in `block in method_missing'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/migration.rb:621:in `block in say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/migration.rb:621:in `say_with_time'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/migration.rb:641:in `method_missing'
/app/db/migrate/20140804010848_create_products.rb:3:in `change'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/migration.rb:595:in `exec_migration'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/migration.rb:579:in `block (2 levels) in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/migration.rb:578:in `block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/migration.rb:577:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/migration.rb:752:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/migration.rb:992:in `block in execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/migration.rb:1038:in `block in ddl_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `block in transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:209:in `within_new_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/connection_adapters/abstract/database_statements.rb:201:in `transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/transactions.rb:208:in `transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/migration.rb:1038:in `ddl_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/migration.rb:991:in `execute_migration_in_transaction'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/migration.rb:953:in `block in migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/migration.rb:949:in `each'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/migration.rb:949:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/migration.rb:807:in `up'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/migration.rb:785:in `migrate'
/app/vendor/bundle/ruby/2.0.0/gems/activerecord-4.1.4/lib/active_record/railties/databases.rake:34:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
Alb
  • 221
  • 2
  • 5
  • 14
  • Related question with similar answer: https://stackoverflow.com/questions/27558540/pgduplicatetable-error – GDP2 Nov 25 '18 at 07:37

2 Answers2

23

Go to Heroku rails console and drop the products (given the they don't contain any data) table by running:

ActiveRecord::Migration.drop_table(:products)

and rerun the migration.

Or just remove the migration file that creates products table from db/migration

This will work as well.

neo
  • 4,078
  • 4
  • 25
  • 41
  • Am I going to be dropping something here that I might want? – Alb Aug 19 '14 at 19:48
  • if this is a new deployment, then you're good, but if you have existing Products, then it'll erase them and you'll have to recreate them, also check edits. – neo Aug 19 '14 at 19:51
  • I believe that I already do have them, which is why I got the error, but I don't understand what I did to make it create another duplicate table. – Alb Aug 19 '14 at 19:59
  • but do you have any data in your products table? – neo Aug 19 '14 at 20:05
  • in my schema create_table "products", force: true do |t| t.string "name" t.float "price" t.text "description" t.string "image" t.string "color" t.datetime "created_at" t.datetime "updated_at" t.integer "product_id" end – Alb Aug 19 '14 at 20:07
  • thats not it, on heroku rails console run Product.all.count if it = 0 then you don't have any data in your products table. – neo Aug 19 '14 at 20:09
  • I have no products yet, they are to be added by admin, just plugging bogus ones in for display/logic testing. Does this mean its safe to kill this table – Alb Aug 19 '14 at 20:12
  • I removed create_products from migrations and re-ran heroku run rake db:migrate, and got the same error. ? – Alb Aug 19 '14 at 20:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59611/discussion-between-neo-and-alex). – neo Aug 19 '14 at 20:19
2

I think you have problem with your schema_migration table in your database which contains timestamp values of each and every migration. If you don't have duplicate migration file for products table then you can truncate the database and run rake db:migrate again. Hope this will work.