How to Turn Off Bigint Primary Keys in Rails 5.1

How to turn off BIGINT primary keys in Rails 5.1

According to pull request, no this is not possible on config level.
But you can, in fact, force id to be integer, like this:

create_table :users, id: :integer do

On the other hand, you must be aware that changes also affected references behavior, so you should be careful with those:

t.references :orders, type: :integer

Seeing as this is too much repeated code, I suggest you write helpers for this, override default methods, or be very radical and fork your database adapter, changing this in it as you like. I'd go with the second option:

  1. Create anonymous modules for Migration[5.0] and ActiveRecord::ConnectionAdapters::TableDefinition
  2. Define create_table, add_reference, add_belongs_to in first one, references and belongs_to in second one (belongs_to ones should be just aliases of references)
  3. In those methods just modify options and call super. Don't forget to handle signatures!
  4. Prepending those modules to their respective classes will handle
    everything for you.
  5. You can go even better and do this for their removal counterparts too.

Rails: How to use BIGINT as Primary Key

If your app was natively built in rails '>= 5.1', your primary keys should already be BIGINT. By "natively built" I mean that your migrations were initially run with that Rails version (as opposed to running them in < 5.1 and then updating the gem later)

If they are not already BIGINT, you can use the migration action found in the source below, pasted here for convenience:

change_column :your_table_name, :id, :bigint

Source: http://www.mccartie.com/2016/12/05/rails-5.1.html

Rails change primary id to 64 bit bigint

While ActiveRecord does not support this, you are able to do it using execute

class UpdateUserIdLimit < ActiveRecord::Migration
def up
# PostgreSQL
execute('ALTER TABLE users ALTER COLUMN id SET DATA TYPE BIGINT')
# MySQL
execute('ALTER TABLE users MODIFY COLUMN id BIGINT(8) NOT NULL AUTO_INCREMENT')
end

def down
raise ActiveRecord::IrreversibleMigration
end
end

For new tables you should be able to simply do

def change
create_table :users, id: false do |t|
t.int :id, limit: 8, primary_key: true
t.string :first_name
t.string :last_name
end
end

Also starting with Rails 5.1 primary keys will be BIGINT by default.

How to turn off auto_increment in Rails Active Record

Try this?

create_table(:table_name, :id => false) do |t|
t.integer :id, :options => 'PRIMARY KEY'
end

rails3 bigint primary key

Had that myself not long ago and found the answer here: Using Rails, how can I set my primary key to not be an integer-typed column?

You need to set primary_key: false and then use a custom statement before you execute the migration.

EDIT 1: You need to check your database docs for the exact query to perform. It is executed as a regular SQL statement and needs to be database specific. The example in the question I referred to is for Postgre SQL. If you are on MySQL you might have to change that.



Related Topics



Leave a reply



Submit