0

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?

3
  • change_column :scores, :test_type, 'boolean USING CAST(column_name AS boolean)' Commented Aug 17, 2014 at 18:26
  • So I revise my migration in rails to match what you have above, then rerun the rake db:migrate? Commented Aug 17, 2014 at 19:41
  • yea. Another option is simply rake db:drop, and rake db:migrate (without the line I recommended). The problem is, I suppose, is that on Heroku you already have some values for test_type, and their type can't be changed. Commented Aug 17, 2014 at 19:56

1 Answer 1

2

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).

Sign up to request clarification or add additional context in comments.

1 Comment

I tried just dropping the DB and re-raking it, but that didn't work for whatever reason. Then I tried your first suggestion and it worked. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.