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?
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
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
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.
Run rake db:migrate
to migrate your database.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With