Alex Rivera | Logout

How to use long id in Rails applications?

Asked 2009-06-30T22:08:14.877
40

How can I change the (default) type for ActiveRecord's IDs? int is not long enough, I would prefer long. I was surprised that there is no :long for the migrations - does one just use some decimal?

Edit
Report

1 Answer

5

If anyone needs this to work with PostgreSQL, create an initializer like this:

# config/initializers/bigint_primary_keys.rb
ActiveRecord::Base.establish_connection
ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:primary_key] = 'bigserial primary key'

Because of lazy loading in Rails 3.2 (and maybe even earlier versions), ActiveRecord::ConnectionAdapters::PostgreSQLAdapter won't be required until you establish the database connection.

answered 2012-08-20T20:30:17.417

Your Answer