Reputation: 1098
I created a migration in my development to change the column type from integer to date in my client table.
Migration:
class UpdateGradToDate < ActiveRecord::Migration
def change
change_column :clients, :grad_id, :date
end
end
Which worked fine in development. However, once I push it to Heroku, I got the following error message.
PG::DatatypeMismatch: ERROR: column "grad_id" cannot be cast automatically to type date
HINT: You might need to specify "USING grad_id::date".
: ALTER TABLE "clients" ALTER COLUMN "grad_id" TYPE date
I'm not sure how to correct this problem. I'm thinking perhaps using rails console and changing the column type there but I'm not sure if that's the best solution.
I also tried creating a new migration using using this syntax as well
class ChangeDateFormatInMyTable < ActiveRecord::Migration
def up
change_column :clients, :grad_id, :integer
end
def down
change_column :clients, :grad_id, :date
end
end
I'm not really sure what to do at this point.
Upvotes: 0
Views: 58
Reputation: 176352
The issue here is clearly explained by the PostgreSQL error:
column "grad_id" cannot be cast automatically to type date
You are trying to convert a field that stores an integer, into a Date. The field could contain anything, including a value such as 10, 0 or -200. How those value could ever be converted into a Date?
What PostgreSQL is telling you is that you need to help him with the conversion.
Your best way is:
grad_id
into grad_id_legacy
grad_id
Date fieldgrad_id_legacy
and stores the corresponding, converted Date value into grad_id
Upvotes: 2