Eric Baldwin
Eric Baldwin

Reputation: 3491

Heroku Rails migration claims an existing column does not exist

A migration that works on my local machine is failing on Heroku. The error is as follows:

Migrating to ChangeTypeToCast (20131112072808)
==  ChangeTypeToCast: migrating ===============================================
-- add_column(:math_tests, :cast, :string)
   -> 0.1006s
-- add_column(:emotional_tests, :cast, :string)
   -> 0.1541s
-- remove_column(:math_tests, :type, :string)
rake aborted!
An error has occurred, this and all later migrations canceled:

PG::Error: ERROR:  column "string" of relation "math_tests" does not exist
: ALTER TABLE "math_tests" DROP "string"/

The migration is removing a column that was created by an earlier migration. The current and earlier migration are both listed below:

Creates the type column in the math_tests table

class CreateMathTests < ActiveRecord::Migration
  def change
    create_table :math_tests do |t|
      t.string :type

      t.timestamps
    end
  end
end

Deletes type column in math_tests (Heroku fails on the first remove_column line)

class ChangeTypeToCast < ActiveRecord::Migration
  def up
    add_column :math_tests, :cast, :string
    add_column :emotional_tests, :cast, :string
    remove_column :math_tests, :type, :string
    remove_column :emotional_tests, :type, :string
  end

  def down
    remove_column :math_tests, :cast, :string
    remove_column :emotional_tests, :cast, :string
    add_column :math_tests, :type, :string
    add_column :emotional_tests, :type, :string
  end
end

Why on earth is this happening?

Note: Running heroku restart and heroku pg:reset did not help

Upvotes: 0

Views: 414

Answers (1)

Bubbles
Bubbles

Reputation: 3815

I'm going to guess that you're running a different version of Rails than the Heroku instance is.

In some versions, you could specify the type of the column as you delete it, which allowed the removal of a column to be reversed. In others, you can specify which table to remove columns from, and then pass a list of column names to delete from. So, rather than trying to delete a column called 'type' of type 'string', it tries to delete a column 'type' as well as a column 'string'.

Relevant release notes - http://guides.rubyonrails.org/4_0_release_notes.html#active-record

Upvotes: 1

Related Questions