2

I was moving my app from Nitrous.io which uses SQLite to Heroku for production which uses Postgres, and I got the following error. 2 things, I don't get why it won't do change_column, and I definitely don't understand their "hint". How can I go about implementing properly?

-- change_column(:messages, :sender, :integer)                                                                                                    
PG::DatatypeMismatch: ERROR:  column "sender" cannot be cast automatically to type integer                                                        
HINT:  Specify a USING expression to perform the conversion.                                                                                      
: ALTER TABLE "messages" ALTER COLUMN "sender" TYPE integer  

The attributes were strings, and I wanted to change to integers so I could reference them. Any help? Below is my db migration that it is referencing:

class ChangeSenderReceiverToInteger < ActiveRecord::Migration
  def change
    change_column(:messages, :sender, :integer)
    change_column(:messages, :receiver, :integer)
  end
end

1 Answer 1

1

Try:

change_column(:messages, :sender, 'integer USING CAST(sender AS integer)')

Ref: http://makandracards.com/makandra/18691-postgresql-vs-rails-migration-how-to-change-columns-from-string-to-integer

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

Comments

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.