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

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)