Tony Tambe
Tony Tambe

Reputation: 573

Error on Heroku trying to change DB column Type

I changed a column in my DB like so:

class ChangeTestTypeInScores < ActiveRecord::Migration
def self.up
 change_column :scores, :test_type, :boolean
end

def self.down
 change_column :scores, :test_type, :string
end
end

It works fine, but when I pushed to heroku and Heroku run rake db: migrate, I get the following error:

PG::DatatypeMismatch: ERROR:  column "test_type" cannot be cast automatically to type     boolean
HINT:  Specify a USING expression to perform the conversion.
: ALTER TABLE "scores" ALTER COLUMN "test_type" TYPE boolean
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

How would I go about correcting this now that I've already updated my local DB?

Upvotes: 0

Views: 352

Answers (1)

Andrey Deineko
Andrey Deineko

Reputation: 52357

The issue is, as I think, that you already have in Heroku database some values of test_type, which are string, and which can not be changed to boolean.

There are two options to overcome the issue.

First: change the migration to:

change_column :scores, :test_type, 'boolean USING CAST(test_type AS boolean)'

and run rake db:migrate.

Second (I would prefer this one more) is to drop the Heroku database and run the migrations again (in the form you wrote it at the very beginning, since there would be no need to cast anything).

Upvotes: 2

Related Questions