A
A
Anton Misyagin2019-09-24 00:41:18
Ruby on Rails
Anton Misyagin, 2019-09-24 00:41:18

Rails doesn't work bigint?

Rails 5.2.3
old migration:

class CreateTmpUsers < ActiveRecord::Migration[5.2]
  def change
    create_table(:tmp_users) do |t|
      t.integer :external_id
      ...
    end
    add_index ...
  end
end

latest migration:
class MigrateToRails52 < ActiveRecord::Migration[5.2]
  def up
    change_column :tmp_users, :external_id, :bigint
...
end

schema.db after migrations:
create_table "tmp_users", force: :cascade do |t|
    t.bigint "external_id"
...
  end

schema.db after rails db:schema:dump
create_table "tmp_users", force: :cascade do |t|
    t.bigint "external_id"
...
  end

in pgadmin I look:
Data type: bigint
TmpUsers.inspect gives
TmpUser(id: integer, external_id: integer , name: string...
Writing to the database via ActiveRecord gives:
message: 392169714724389808 is out of range for ActiveModel::Type::Integer with limit 4 bytes
error class: ActiveModel::RangeError

Well, why does the rail not understand that 8 bytes, not 4

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Shakhunov, 2019-09-27
@inf

https://stackoverflow.com/a/3736425/1697148
Here's the magic incantation in your migration when you declare the column:

create_table :example do |t|
  t.integer :field, :limit => 8
end

It seems that they fixed it in the 6th rail .

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question