Postgres DB上的整数超出范围

时间:2022-08-03 00:13:16

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

5 个解决方案

#1


19  

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

#2


56  

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字节有符号整数。

#3


12  

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: 8 will also achieve the same thing

如前面的答案所述,限制:8也会达到同样的效果

#4


1  

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。

#5


1  

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,除非你需要更大的大小或小数,因为它要快得多。

#1


19  

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

#2


56  

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字节有符号整数。

#3


12  

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: 8 will also achieve the same thing

如前面的答案所述,限制:8也会达到同样的效果

#4


1  

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。

#5


1  

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,除非你需要更大的大小或小数,因为它要快得多。