I have run a migration when trying to change on heroku table column from string to integer: This is my migration:
class ChangePriceTypeInItems < ActiveRecord::Migration
def change
change_column :items, :price, :integer
end
end
And this is my error: What do I do?
ActiveRecord::StatementInvalid: PG::Error: ERROR: column "price" cannot be cast automatically to type integer
HINT: You might need to specify "USING price::integer".
: ALTER TABLE "items" ALTER COLUMN "price" TYPE integer
The other answers are correct, yet you can also use the :using keyword:
change_column :items, :price, :integer, using: 'price::integer'
If you're sure the data in the string column can be converted to integer then go ahead with this change to your migration:
change_column :items, :price, 'integer USING CAST(price AS integer)'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With