Search code examples
ruby-on-railspostgresqlintegerbignum

Rails Postgreql change int to bignum on column


I have a field in a table that was originally set as integer. It is an ID from a an external data source. Now using multiple datasources some of the ID values are very large integers, larger than postgres 4bit default. What is the best way to change the table structure?


Solution

  • This migration using striaght SQL did the trick for me. Thanks to: https://gist.github.com/lxxdn/1e23067da51072a7e446

    class ChangeForignIdsToBigInt < ActiveRecord::Migration
    
      def up
        execute <<-SQL
          ALTER TABLE events
          ALTER COLUMN eid TYPE bigint USING eid::bigint
        SQL
        execute <<-SQL
          ALTER TABLE venues
          ALTER COLUMN vid TYPE bigint USING vid::bigint
        SQL
      end
    
      def down
        raise ActiveRecord::IrreversibleMigration.new
      end
    
    end