29

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

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I'd rather recommend to change/set a correct data type in the table's field
20

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

See 8.1. Numeric Types.

1 Comment

Thanks nsanders. 'bigint' is what I was looking for. Sorry if I phrased the question poorly everyone!
12

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.

Comments

2

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.

Comments

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.

2 Comments

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 not offhand, but I bet some other question will answer that; if not, ask a new one

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.