Newy
Newy

Reputation: 40107

Error message "Integer out of range" in PostgreSQL database

This is a a simple Ruby on Rails application using a PostgreSQL database, and I am getting 'integer out of range' error when trying to insert 2176968859. It should be an easy fix to the migrations, but I'm not sure. Right now I've got...

create_table :targets do |t|
   t.integer :tid
    ...
end

Upvotes: 29

Views: 35923

Answers (5)

Christian Fazzini
Christian Fazzini

Reputation: 19723

In Ruby on Rails 4, in your migration file, you could define the column as:

t.column :foobar, :bigint

As noted in previous answers, limit: 8 will also achieve the same thing.

Upvotes: 12

Magnus Hagander
Magnus Hagander

Reputation: 25098

PostgreSQL integers are signed, and there aren't any unsigned datatype. I bet that's your problem.

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.

Upvotes: 2

nsanders
nsanders

Reputation: 12636

You are overflowing. Use a bigint if you need numbers that big.

See 8.1. Numeric Types.

Upvotes: 20

Paul Rubel
Paul Rubel

Reputation: 27222

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.

Upvotes: 57

ysth
ysth

Reputation: 98398

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.

Upvotes: 1

Related Questions