Ruby on Rails ignores integer limit

I need to index a user table using an identifier from an external source, which is a 64-bit integer. Rails is very good at storing that amount, unless it's a primary key. I have the following migration:

class CreateUsers < ActiveRecord::Migration def change create_table :users, :id => false do |t| t.integer :id, limit: 8 t.string :name t.timestamps null: false end end end 

Migration works fine, no errors are reported, but when I try to sow it using a 64-bit integer, they tell me:

 RangeError: 76561198054432981 is out of range for ActiveRecord::Type::Integer with limit 4 

Obviously, Rails ignores the marginal field if this is the primary key field /: id? How can I handle this?

For what I use sqlite3 (by default), but as far as I know, sqlite is perfectly capable of storing 64-bit integers.

Here's table_info from sqlite:

 0|id|integer(8)|0||0 1|name|varchar|0||0 2|created_at|datetime|1||0 3|updated_at|datetime|1||0 
+6
source share
1 answer

The limit value that you have given is true; it corresponds to the BIGINT type enter image description here Make sure your migration applies; open your database in some CLI or GUI programs and check col type

Addition: Changing the column length or data type during migration will invalidate the column as the primary key. Rather, creating an initializer that overrides the default primary key data type should provide the behavior you want to implement:

 # config/initializers/change_primary_key_datatype.rb require 'active_record/connection_adapters/postgresql_adapter' ActiveRecord::ConnectionAdapters::PostgreSQLAdapter::NATIVE_DATABASE_TYPES[:primary_key] = "bigserial primary key" 

This is what we will do for the PG database; It is possible because

enter image description here


however, the SQLite code base has

enter image description here

+3
source

All Articles