I'm working on a Rails 3 app and am trying to run a migration. I am trying to create a table called Songs and I was able to successfully create the table and migrate locally. However, when I push the code to Heroku and migrate there, the error I encounter is:
** Execute db:migrate
== CreateSongs: migrating ====================================================
-- create_table(:songs)
NOTICE: CREATE TABLE will create implicit sequence "songs_id_seq1" for serial column "songs.id"
rake aborted!
An error has occurred, this and all later migrations canceled:
PG::DuplicateTable: ERROR: relation "songs" already exists
: CREATE TABLE "songs" ("id" serial primary key, "solo_cello" character varying(255), "created_at" timestamp, "updated_at" timestamp) /app/vendor/bundle/ruby/1.9.1/gems/activerecord-3.1.12/lib/active_record/connection_adapters/postgresql_adapter.rb:605:in `async_exec'
I realize this error stems from the table already existing in the database, however when I run
heroku run "bundle exec rake db:schema:dump && cat db/schema.rb"
I see that the schema version is at ActiveRecord::Schema.define(:version => 20130915155113) and the migration to create the Songs model occurs after this migration (it is 20140506043817).
Here's is a truncated version of my local schema:
ActiveRecord::Schema.define(:version => 20140511155456) do
[other tables...]
create_table "songs", :force => true do |t|
t.string "solo_cello"
t.datetime "created_at"
t.datetime "updated_at"
t.string "solo_violin"
t.string "duo_one"
t.string "duo_two"
t.string "trio_a_one"
t.string "trio_a_two"
t.string "trio_a_three"
t.string "trio_b_one"
t.string "trio_b_two"
t.string "trio_b_three"
t.string "quartet_one"
t.string "quartet_two"
t.string "quartet_three"
t.string "quartet_four"
end
Here are my questions:
How does the table already exist if the schema on the database is at version 20130915155113 if the migration that creates the Song table (20140506043817) comes later?
What do I need to do to run a successful migration? I realize I can drop the database (this is the staging environment) but would greatly prefer not to do that on production.
Why do migrations work fine on my machine locally but not on Heroku?