PostgreSQL 数据库中的整数超出范围

编程入门 行业动态 更新时间:2024-10-24 22:18:59
本文介绍了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 数据库中的整数超出范围

本文发布于:2023-10-16 12:15:57,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497526.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:整数   数据库中   PostgreSQL

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!