PostgreSQL 数据库中的整数超出范围 [英] Integer out of range in PostgreSQL database

查看:36
本文介绍了PostgreSQL 数据库中的整数超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试保存一个代表文件长度的数字 (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

我应该为这个值使用一些其他的列类型吗?(在导轨 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 个字节作为长整数(a 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆