29

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...

create_table :targets do |t|
   t.integer :tid
    ...
end
Rohit
  • 5,631
  • 4
  • 31
  • 59
Newy
  • 38,977
  • 9
  • 43
  • 59

5 Answers5

57

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.

Paul Rubel
  • 26,632
  • 7
  • 60
  • 80
20

What's the question? You are overflowing. Use a bigint if you need numbers that big.

http://www.postgresql.org/docs/8.3/interactive/datatype-numeric.html

nsanders
  • 12,250
  • 2
  • 40
  • 47
  • Thanks nsanders. 'bigint' is what I was looking for. Sorry if I phrased the question poorly everyone! – Newy Jun 16 '09 at 16:11
12

In 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

Christian Fazzini
  • 19,613
  • 21
  • 110
  • 215
2

PostgreSQL integers are signed, there is no 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.

Magnus Hagander
  • 23,890
  • 5
  • 56
  • 43
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.

ysth
  • 96,171
  • 6
  • 121
  • 214
  • nice, any Idea how I can get the max number for integer programmatically so I can set to nil if anyone enters a huge number? I use the pg gem in rails. – ryan2johnson9 Oct 10 '17 at 03:07
  • @ryan2johnson9 not offhand, but I bet some other question will answer that; if not, ask a new one – ysth Oct 10 '17 at 19:07