The structure of my database in development was changed, so I tried to reset the deployed Heroku database. Took it off, but now I can’t migrate my db

so my application worked fine. I created a new model and some associations, providing all the old seed data in my heroku application is useless. so I tried reset it and filled it again. but I can't even transfer my db to a hero using the heroku rake db:migrate command. I am 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 application is working fine. I added a new model and decided that I should reset the deployed database. So, I ran heroku pg:reset , and then clicked my code on the hero. then tried to migrate db, but it does not work! What I've done? I have been trying to figure this out for the last 4 hours. I can’t think straight. Any help would be greatly appreciated.

+4
source share
2 answers

According to heroku docs here

Reason: PostgreSQL does not know how to attribute all rows in this table to the specified type. Most likely, this means that you have an integer or a row in this column.

Decision. Inspect your records and make sure they can be converted to a new type. Sometimes it’s easier to avoid using change_column, rename / create a new column.

I have also been in this position before, and I could not find a solution, so I had to create a new column, postgresql seems sensitive when I come to these problems. Hope this helps.

+4
source

In continuation of the answer of Hishalva. If you cannot use "change_column", you can go roundabout:

  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 

This is a circular motion, and Email.all.each may be slow, but it will work and give all the best for you.

If you intend to use it only in prod, then you can replace the ruby ​​updates with the SQL UPDATE command, which does the same in the database using the Cast-POSTGRES method.

+8
source

Source: https://habr.com/ru/post/1386297/


All Articles