Search code examples
ruby-on-railsruby-on-rails-3herokusqliteheroku-postgres

heroku run rake db:migrate gives rake aborted


I'm new to ruby on rails, github and heroku. Now i am doing fixed deposit project in that project i am using database sqlite3 in my localhost and postgres in heroku.

I have add a field called int which datatype is string and i changed the datatype from string to integer. Finally, i decided to change it again from integer to float. In my local host its working perfecly. But, when i'm trying to run in heroku it shows rake aborted.

heroku run rake db:migrate

it shows the following error message

Running `rake db:migrate` attached to terminal... up, run.4356

Connecting to database specified by DATABASE_URL

Migrating to CreateFds (20140505120047)

Migrating to Changedatatypeforfd (20140506065307)


Migrating to AddMdToFds (20140506080333)

Migrating to AddIntToFds (20140506080404)

Migrating to Changedatatypeforint (20140506103001)

==  Changedatatypeforint: migrating ======================

-- change_column(:fds, :int, :integer)

rake aborted!

StandardError: An error has occurred, this and all later migrations canceled:

PGError: ERROR:  column "int" cannot be cast automatically to type integer

HINT:  Specify a USING expression to perform the conversion.
: ALTER TABLE "fds" ALTER COLUMN "int" TYPE integer

my db files are listed below

20140506080404_add_int_to_fds.rb

class AddIntToFds < ActiveRecord::Migration

  def change

    add_column :fds, :int, :string
  end

end

20140506103001_changedatatypeforint.rb

class Changedatatypeforint < ActiveRecord::Migration

 def up

    change_column :fds, :int, :integer

  end


  def down

    change_column :fds, :int, :string


  end

end

20140508105541_rechangedatatypeforint.rb

class Rechangedatatypeforint < ActiveRecord::Migration


  def up

    change_column :fds, :int, :float

  end


  def down

    change_column :fds, :int, :integer

  end

end

Sorry for my blunder. Please give me the solution. Thanks in advance.


Solution

  • Heroku uses PostgreSQL. In PostgreSQL int is a reserved word, you cannot use it as a column name. Use something else for your int name

    change_column :fds, :valid_name, :integer
    

    SQL Key Words

    If you want to cast from float to integer, one way to do it is:

    class MigrationNameHere < ActiveRecord::Migration
      def up
        execute('ALTER TABLE fds ALTER COLUMN column_name TYPE integer USING round(column_name);')
      end
    end
    

    This migration will convert all your float numbers to integer ones and it is not reversible so be careful. I advise you to have a backup before doing this.