Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Altered my database structure in development, so tried to reset Heroku deployed database. Erased it but now I can't migrate my db over

so my app was working fine. I created a new model and some associaitons, rendering all the old seed data in my heroku app useless. so I tried to reset it and populate it again. but I can't even migrate my db to heroku with the heroku rake db:migrate command. I'm using SQLite, but it seems my error is related to Postgres. I don't know what that means, if anything

Here is the error:

rake aborted!
An error has occurred, this and all later migrations canceled:

PGError: ERROR:  column "to" cannot be cast to type "pg_catalog.int4"
: ALTER TABLE "emails" ALTER COLUMN "to" TYPE integer

Tasks: TOP => db:migrate

My migration:

class ChangeDataTypeForEmailUsers < ActiveRecord::Migration
  def self.up
      change_column :emails, :to, :integer
      change_column :emails, :from, :integer
  end

  def self.down
      change_column :to, :string
      change_column :from, :string
  end
end

What is the problem? My deployed app was working fine. I added a new model and figured I should reset the deployed database. So I ran heroku pg:reset then pushed my code to heroku. then tried to migrate the db, but it doesn't work! What have I done? I have been trying to figure this out for the last 4 hours. I can't think straight anymore. Any help would be greatly appreciated

like image 272
Brian Avatar asked Dec 16 '22 06:12

Brian


1 Answers

Further to Hishalv's answer. If you can't use "change_column" then you can go the roundabout way:

 def self.up
   rename_column :emails, :to, :old_to
   add_column :emails, :to, :integer
   Email.reset_column_information
   Email.all.each {|e| e.update_attribute(:to, e.old_to.to_i) }
   remove_column :emails, :old_to
 end

It's roundabout, and the Email.all.each might be slow - but it'll work and correctly cast for you.

If you're only going to use it on prod, then you might be able to replace the ruby-updates with a SQL UPDATE command that does the same thing in-database using a POSTGRES-specific cast method.

like image 156
Taryn East Avatar answered May 08 '23 08:05

Taryn East