← homeProgramming (Програмування)

What does the error 'is out of range' mean in Ruby on Rails? Range Error - Integer with limit 4 bytes

What does the error 'is out of range' mean in Ruby on Rails? Range Error. ActiveModel::Type::Integer with limit 4 bytes

This content has been automatically translated from Ukrainian.
RangeError can be seen in Ruby on Rails after attempting to write a number that is too large (or too small) to the database. The error may look like this:
999999999999999999999999999999999999999 is out of range for ActiveModel::Type::Integer with limit 4 bytes
or 
999999999999999999999999999999999999999 is out of range for ActiveRecord::Type::Integer with limit 4
This means that we are trying to shove something into the database that is outside the allowed range.
For numbers in the database, we can use the data types int and bigint. Each of these types has its own limitations. I have already written about this in the post about the difference between int and bigint.
I'll duplicate it here:
  • Int can have values between -2147483648 and 2147483647.
  • Bigint between -9223372036854775808 and 9223372036854775807.
So these are architecture-fixed limits for int (32-bit number) and bigint (64-bit number).
The error can be resolved by using numbers within these ranges. Sometimes this error occurs when testing inputs on the frontend. QA enters a large number into the field and we get a RangeError. This is usually fixed by adding validations and limits (both on the fe and be sides).
But what to do if this is a model id and we need a larger range of possible values? There are actually several options.
  • Transitioning from ID to UUID (Universally Unique Identifier). There can be quite a few variations of implementation, depending on the stack and scope.
  • Sharding the database into clusters. There are also many variations of implementations here. But conditionally - each cluster (shard) will have a prefix and its own range of bigint.
  • In large systems, you can see composite keys that are created from several different columns. But this is also a specific option, the implementation of which depends on many factors.

🔥 More posts

All posts
Scope of a local variable in Ruby
Programming (Програмування)Jun 3, '24 16:46

Scope of a local variable in Ruby

The scope of local variables in Ruby. Examples of how variable visibility works in Ruby.

What is immutability and mutability?
Programming (Програмування)Jun 19, '24 07:48

What is immutability and mutability?

What is immutability and mutability? A description of the term in the context of programming and ...

What is a function in programming?
Programming (Програмування)Jun 24, '24 18:15

What is a function in programming?

What is a function in programming? Example of a function in Ruby and JavaScript

How to make an empty git commit?
Programming (Програмування)Jun 28, '24 08:33

How to make an empty git commit?

How to make an empty git commit? Let's make an empty git commit. Why? Who knows, everyone has the...