Search code examples
ruby-on-railsactiverecordcommand-linemigrationheroku-postgres

change an integer to bigint in ROR


I need to change my table integers to BIGINT's I am using RoR and SQLLite.

How can I modify existing table fields from Int to BigInt through command line?


Solution

  • You can do that with a migration. Here's a link to the docs: http://edgeguides.rubyonrails.org/active_record_migrations.html

    Basically, you need to run:

    rails g migration change_your_column_to_bigint
    

    and add this code to the migration new migration file:

    def up
      execute <<-SQL
        ALTER TABLE ip_to_countries
        ALTER COLUMN your_column TYPE bigint USING your_column::bigint
      SQL
    end
    

    For the record, I stole this from here: How do I change a string column into a bigint?, but didn't mark it as duplicate because it involves converting a string column.

    now I am getting this error

    SQLite3::SQLException: near "ALTER": syntax error:     ALTER TABLE posts
        ALTER COLUMN mileage TYPE bigint USING mileage::bigint
    C:/Sites/jeepjig/db/migrate/20150315145856_change_mileage_to_bigint.rb:3:in `change'
    C:in `migrate'
    ActiveRecord::StatementInvalid: SQLite3::SQLException: near "ALTER": syntax error:     ALTER TABL
        ALTER COLUMN mileage TYPE bigint USING mileage::bigint
    C:/Sites/jeepjig/db/migrate/20150315145856_change_mileage_to_bigint.rb:3:in `change'
    C:in `migrate'
    SQLite3::SQLException: near "ALTER": syntax error
    C:/Sites/jeepjig/db/migrate/20150315145856_change_mileage_to_bigint.rb:3:in `change'
    C:in `migrate'
    Tasks: TOP => db:migrate
    (See full trace by running task with --trace)
    

    after running this

    class ChangeMileageToBigint < ActiveRecord::Migration
      def change
      execute <<-SQL
        ALTER TABLE posts
        ALTER COLUMN mileage TYPE bigint USING mileage::bigint
      SQL
    end
    end