6

I need to index a table of users using an externally sourced id, which is a 64-bit integer. Rails is perfectly capable of storing such a number, unless it's the primary key it seems. 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

The migration works fine, no errors reported, but when I attempt to seed it with a 64-bit integer, I'm told off by this:

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

Obviously Rails is ignoring the limit field, so long as it's the primary key/the :id field? How should I go about dealing with this?

For what it's worth I'm using sqlite3 (default), but to my knowledge, sqlite is perfectly capable of storing 64-bit integers.

Here's the 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
3
  • 1
    Are you sure this isn't a SQLite issue? jakegoulding.com/blog/2011/02/06/sqlite-64-bit-integers Commented Oct 25, 2015 at 2:51
  • @CristianoBetta I don't believe so, I tried adding another column with the same specs under a different name which worked fine. According to pragma table_info both are defined in the same way. Commented Oct 25, 2015 at 3:04
  • I have a similar issue on PostgreSQL. The database states that the column is bigint and the column metadata states the limit of 8, but I get the error of out of range for ActiveModel::Type::Integer with limit 4 Commented Dec 27, 2016 at 18:16

1 Answer 1

4

The limit value you gave is correct; it corresponds to BIGINT type enter image description here Make sure your migration is applied; open you database in some CLI or GUI software and verify the col-type

Addition: Changing a column's length or datatype in a migration will invalidate the column as a primary key. Rather, creating an initializer that overrides the site's default primary key datatype should provide the behavior you're looking 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 would do for PG database; This is possible because of

enter image description here


however in the code base of SQLite there is

enter image description here

Sign up to request clarification or add additional context in comments.

4 Comments

I removed the database, ran the migration, tried seeding it, and the error remains. sqlite shows the correct and expected :limit.
So there's no driver-agnostic way to change the size of the primary key?
Why include screen shots of text when you could have pasted in the text (and linked to the documentation) with less effort?
@muistooshort :: I needed to highlight few words so I thought it would be better

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.