Ruby-on-rails Postgres DB 上的整数超出范围
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/999570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Integer out of range on Postgres DB
提问by Newy
Simple rails app using Postgres DB, getting 'integer out of range' error when trying to insert 2176968859. Should be an easy fix to the migrations, but I'm not sure. Right now I've got...
使用 Postgres DB 的简单 Rails 应用程序,在尝试插入 2176968859 时出现“整数超出范围”错误。应该可以轻松解决迁移问题,但我不确定。现在我有...
create_table :targets do |t|
t.integer :tid
...
end
采纳答案by nsanders
What's the question? You are overflowing. Use a bigint if you need numbers that big.
问题是什么?你溢出来了。如果您需要那么大的数字,请使用 bigint。
http://www.postgresql.org/docs/8.3/interactive/datatype-numeric.html
http://www.postgresql.org/docs/8.3/interactive/datatype-numeric.html
回答by Paul Rubel
Here's the magic incantation in your migration when you declare the column:
当您声明列时,这是迁移中的魔法咒语:
create_table :example do |t|
t.integer :field, :limit => 8
end
The :limit => 8 is the magic in this case as postgres only does signed 4-byte integers when you just say integer. This uses 8-byte signed integers.
:limit => 8 在这种情况下很神奇,因为当你只说整数时,postgres 只处理有符号的 4 字节整数。这使用 8 字节有符号整数。
回答by Christian Fazzini
In Rails 4. In your migration file, you could define the column as:
在 Rails 4 中。在您的迁移文件中,您可以将列定义为:
t.column :foobar, :bigint
As noted in previous answers, limit: 8will also achieve the same thing
如之前的答案所述,limit: 8也将达到同样的目的
回答by Magnus Hagander
PostgreSQL integers are signed, there is no unsigned datatype - I bet that's your problem.
PostgreSQL 整数是有符号的,没有无符号数据类型 - 我敢打赌这是你的问题。
If you need larger values, use bigint. If bigint also isn't enough, use numeric - but use bigint rather than numeric unless you need the larger size or decimals, since it's much faster.
如果需要更大的值,请使用 bigint。如果 bigint 还不够,请使用 numeric - 但使用 bigint 而不是 numeric 除非您需要更大的尺寸或小数,因为它要快得多。
回答by ysth
Note the range of allowed values for the integer type in http://www.postgresql.org/docs/8.3/interactive/datatype-numeric.html. I think you are going to have to use a bigint, decimal, or double precision.
请注意http://www.postgresql.org/docs/8.3/interactive/datatype-numeric.html 中整数类型的允许值范围。我认为您将不得不使用 bigint、decimal 或 double precision。

