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:
- Create anonymous modules for
Migration[5.0]
andActiveRecord::ConnectionAdapters::TableDefinition
- Define
create_table
,add_reference
,add_belongs_to
in first one,references
andbelongs_to
in second one (belongs_to
ones should be just aliases ofreferences
) - In those methods just modify options and call super. Don't forget to handle signatures!
- Prepending those modules to their respective classes will handle
everything for you. - 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
Pg_Dump: [Archiver (Db)] Query Failed: Error: Permission Denied for Relation Abouts
Defined' and 'Unless' Not Working as Expected
Ruby Getting Deeply Nested JSON API Data
Is /Etc/Irbrc Installed by Os X? Does Irb Read It
Ruby Symbol#To_Proc Leaks References in 1.9.2-P180
Are There Any Additional Inject Shorthand
Different Behaviour of 'Do .. End' and "{..}" Block in Ruby
Ruby- Delete a Value from Sorted (Unique) Array at O(Log N) Runtime
Should Repeat a Number of Times
How to Create Alias to Attributes in Ruby
Ruby Popen3 -- How to Repeatedly Write to Stdin & Read Stdout Without Re-Opening Process
Deep Convert Openstruct to JSON
How to Combination/Permutation in Ruby
Differencebetween a Constant and a Variable in Ruby