Rails 6, float or bigint column types being treated as integer by ActiveRecord

Viewed 1013

I need to accept very large integer values on a postgres column on a table pre_transaction. The column name is give_amount.

I tried changing the column type to bigint with:

change_column :pre_transactions, :give_amount, :bigint

but I still get the following error when I try to save the record even after restarting the console:

ActiveModel::RangeError (1000000000000000000000 is out of range for ActiveModel::Type::Integer with limit 8 bytes)

I also realized that if I run PreTransaction in the rails console, the bigint columns are shown as integer columns (same for foreign keys that are stated as bigint in the schema but appear as integer on rails c), which is weird.

That's why I then changed the column type to float. Now when I run PreTransaction in the rails console I do see the column as type float but now when I try to save the record it saves, but rounding up to 1.0.

So, how can I do to store very large integers in the database?

1 Answers

Instead of :bigint use the :decimal type. It avoids float rounding inconsistencies and allows for large numbers to be ok.

Related