PatrickLightning
PatrickLightning

Reputation: 117

Rails migration for BigInt fails when using Postgres

I'm trying to get a column in my database to use bigint as the type.

I have tried several combinations already including these with no luck:

add_column :my_table, :my_field, :bigint
add_column :my_table, :my_field, :bigint, limit: 8
add_column :my_table, :my_field, :integer, limit: 8

I think part of the issue is that this is not just a Rails migration for a DB column to be bigint, but it also needs to work with PostGres.

Upvotes: 0

Views: 663

Answers (1)

Andrew Hendrie
Andrew Hendrie

Reputation: 6415

I think there's a couple of ways to do this:

1. Using a limit of 8 with the integer type.

create_table 'example' do |t|
  t.integer :my_field, :limit => 8
end

See this page for more information on using limits to set integer sizing in Rails.

2. Using t.column with a bigint type

For some reason the create table doesn't like bigint. You can, however do it with add_columm using the bigint data type:

add_column :table_name, :really_big_int, :bigint

In Rails 4 with the pg gem this works:

class CreateDemo < ActiveRecord::Migration
  def self.up
    create_table :my_table do |t|
      t.column :my_field, :bigint
    end
  end
end

See this answer for more details on that.

This answer assumes that you're using the pg gem in your Rails project and that you've got a postgres database created and accessible in your development environment (specified in app/config/database.yml).

Upvotes: 1

Related Questions