Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting error indicating number is "out of range for ActiveRecord::Type::Integer with limit 4" when attempting to save large(ish) integer value

I'm using SQLite + ActiveRecord in my Ruby app, and here's the error I get while trying to write a big number to the integer field:

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

But according to the SQLite documentation:

The value is a signed integer, stored in 1, 2, 3, 4, 6, or 8 bytes depending on the magnitude of the value.

8 bytes is a plenty of space to store the integer 1428584647765, so why does ActiveRecord give me an error? Why does it think that this is a 4-byte field?

like image 756
Sergey Avatar asked Apr 09 '15 12:04

Sergey


2 Answers

I ran into the same problem, and the answer above gave me a clue on how I fixed mine. I propose my answer a little detailed to solving the problem.

You can do this by setting limit on your table column.

Hack/Steps

  1. Run a migration to change your table column. e.g.

    rails generate migration change_integer_limit_in_your_table

    Note: your_table in the code will be your table name in plural

  2. Edit the generated migration such that the generated migration will look like this:

    class ChangeIntegerLimitInYourTable < ActiveRecord::Migration    def change      change_column :your_table, :your_column, :integer, limit: 8    end   end 

    Note: The limit of 8 in the code is the storage size which can range from -9223372036854775808 to +9223372036854775807 and called bigint i.e. large-range integer.

  3. Run rake db:migrate to migrate your database.

  4. Restart your server by running rails server and you are up and running.

For more information on Numeric Type, see https://www.postgresql.org/docs/9.4/static/datatype-numeric.html

like image 172
Afolabi Olaoluwa Akinwumi Avatar answered Sep 25 '22 07:09

Afolabi Olaoluwa Akinwumi


Good day. By default columnt create with len = 32 bytes

for change this, you can create migration, for example:

t.integer :some_field, :limit => 8 
like image 25
diderevyagin Avatar answered Sep 26 '22 07:09

diderevyagin