PostgreSQL数据库中的整数超出范围 [英] Integer out of range in PostgreSQL database
问题描述
我正在尝试保存一个表示文件长度的数字(4825733517)。该列设置为整数。我没有设置任何验证或限制。
I'm trying to save a number representing the length of a file (4825733517). The column is set to type integer. I don't have any validations or restrictions set.
RangeError: 4825733517 is out of range for ActiveRecord::ConnectionAdapters::PostgreSQL::OID::Integer with limit 4
我是否应该为此值使用其他一些列类型? (在Rails 4.2.4上)
Should I be using some other column type for this value? (on rails 4.2.4)
推荐答案
对于类型为 integer
的列,:limit
值是最大列长度(以字节为单位)(文档)。
For columns of type integer
, the :limit
value is the maximum column length in bytes (documentation).
具有4个字节的长度,可以存储的最大有符号整数为2,147,483,647,比您的值4,825,733,517小得多。您可以将字节限制增加,例如增加到8个字节,以成为一个长整数( bigint PostgreSQL类型),那么您最多可以存储带符号的值9,223,372,036,854,775,807。
With 4 byte length, the largest signed integer you can store is 2,147,483,647, way smaller than your value of 4,825,733,517. You can increase the byte limit, for example to 8 bytes to be a long integer (a bigint PostgreSQL type), this will allow you to store signed values up to 9,223,372,036,854,775,807.
您可以通过迁移来做到这一点,使用 rails generate migration change_integer_limit_in_your_table
和以下代码创建它:
You can do this with a migration create it with something like rails generate migration change_integer_limit_in_your_table
, and the following code:
class ChangeIntegerLimitInYourTable < ActiveRecord::Migration
def change
change_column :your_table, :your_column, :integer, limit: 8
end
end
这篇关于PostgreSQL数据库中的整数超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!