1

Rails 5.2.3 + postgresql 11.5 + gem 'pg', '1.1.4'. My first migration:

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

second migration:

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

schema.db after rails db:migrate:

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

inside pgadmin I see: Type: bigint

TmpUser.inspect gives:

TmpUser(id: integer, external_id: integer, name: strin...

Save through ActiveRecord gives:

TmpUser.create external_id: 392169714724389808

message: 392169714724389808 is out of range for ActiveModel::Type::Integer with limit 4 bytes error class: ActiveModel::RangeError

I need 8 bytes, but got 4 in validation

  • gem 'activerecord-suppress_range_error' - partically solve my problem. There is new one: PG::NumericValueOutOfRange: ERROR: value "392169714724389808" is out of range for type integer gems/activerecord-5.2.3/lib/active_record/connection_adapters/postgresql_adapter:611 – Anton Misyagin Sep 24 '19 at 11:18
  • How did you solve your problem? – Tortoise Jan 19 '22 at 00:55

1 Answers1

0

For some reason the create table doesn't like bigint. You can, however do it with add_columm using the bigint data type:

add_column :table_name, :really_big_int, :bigint

Source: - Rails Migration: Bigint on PostgreSQL seems to be failing?

Rakesh
  • 793
  • 4
  • 22
  • Now I have database with data, which I don't want destroy. I have column type bigint in database. I have migrations history where I can edit change_column to add_column. I have result schema.db: create_table "tmp_users", force: :cascade do |t| t.bigint "external_id" ... end Additionally rails on init read database to calculate schema.rb and I can remove schema.db whith no affect to my application. So if I want to setup my model manualy what I must do for this? – Anton Misyagin Sep 24 '19 at 11:33