Forum: Ruby on Rails How can I create a column of bigint sql type in migration?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
1292dfc5df2064e008f7f12b9c56561d?d=identicon&s=25 allen (Guest)
on 2007-04-09 06:27
(Received via mailing list)
Hi all,

I want to use bigint sql type for a model's id. How can I do this in
migration? Should I just use the integer ruby type?
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (au5lander)
on 2007-04-09 13:15
(Received via mailing list)
i would think that the standard INT column type would be enough, as,
even when signed, allows for values over 2 billion (over 4 billion
unsigned) where a BIGINT would allow for over 9 quintillion
(9,000,000,000,000,000,000).

so if you really do need a BIGINT data type, there is no way to do it
via standard migration functionality.

you can issue an alter table command and change the column type however.

self.up
  ...
  execute("ALTER TABLE things CHANGE column_name column_name BIGINT")
end
5030981121b21bed8aee074f68bd5074?d=identicon&s=25 Russell Norris (Guest)
on 2007-04-09 16:58
(Received via mailing list)
Not to call you out Chris but have you ever tried

class CreateThings < ActiveRecord::Migration
  def self.up
    create_table :things do |t|
      t.column :my_big_int, :bigint
      t.column :my_medium_text, :mediumtext
      # You can even specify limits!
      t.column :my_char_64, :"char(64)"
    end
  end

  def self.down
    drop_table :things
  end
end

in your migrations. I think you'd be pleasantly surprised by the
results.
Yes, it's database specific. Yes, it doesn't have DDH's imprimatur. But
sometimes it pays to be agile. :) The only downside I saw was that the
schema.rb file doesn't correctly reflect the actual database. What MySQL
truly recognizes as CHAR(64) is label in the schema as :string, :limit
=>
64, which _is_ database agnostic but not exactly true to the DB. What
this
would entail in actual practice? I dunno. I've never used the
schema.rb[that I know of] so it's possible that there are no drawbacks
but I thought
in the interest of full disclosure that I'd let y'all know.

On 4/9/07, Chris Hall <christopher.k.hall@gmail.com> wrote:

if you really do need a BIGINT data type, there is no way to do it
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (au5lander)
on 2007-04-09 17:46
(Received via mailing list)
you're right, i've never used :bigint, as I was going by the
documentation at

http://api.rubyonrails.org/classes/ActiveRecord/Co...

which states that only the following

:primary_key, :string, :text, :integer, :float, :decimal, :datetime,
:timestamp, :time, :date, :binary, :boolean

are allowed parameters when specifying column type.
5030981121b21bed8aee074f68bd5074?d=identicon&s=25 Russell Norris (Guest)
on 2007-04-09 17:58
(Received via mailing list)
I think that the API is aimed at being as db-agnostic as you can. Which
I
completely _do_ agree with. I was just trying to share the wealth with
my
twocents. Sometimes you have situations that don't have to be
db-agnostic
and if you're aware of the pros and cons you should go where agile takes
you.

RSL
A2c85dc5ee81b12e3cc0a6522e8d079d?d=identicon&s=25 Chris Hall (au5lander)
on 2007-04-09 19:30
(Received via mailing list)
I agree whole-heartedly.  There are quite a bit of hidden/unknown
features that can definitely be taken advantage of if you know about
them and the pros outweigh the cons.
771f52b5b1d9889ded1391e182b44dbb?d=identicon&s=25 Milli Sami (millisami)
on 2009-08-29 23:19
allen wrote:
> Hi all,
>
> I want to use bigint sql type for a model's id. How can I do this in
> migration? Should I just use the integer ruby type?

Just use
  t.integer :foo, :limit => 8
19990e3fa0fc68c874f42f13d1500011?d=identicon&s=25 Steve Rawlinson (trip)
on 2009-09-15 21:46
>> I want to use bigint sql type for a model's id. How can I do this in
>> migration? Should I just use the integer ruby type?
>
> Just use
>   t.integer :foo, :limit => 8

I'm pretty sure that doesn't do what you think it does. It creates a an
int(8) in mysql (as opposed to the default which is int(11)) but this
does not affect the size of the number that can be stored in the field.
An int field in mysql is 32 bits long regardless of the :limit you set
(that's only the 'display' length).

If you want to store numbers greater than 2147482624 you hvae to use
bigint.

steve
This topic is locked and can not be replied to.