Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integer out of range on Postgres DB

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 
like image 353
Newy Avatar asked Jun 16 '09 04:06

Newy


People also ask

What is integer out of range?

Explanation: You've tried to INSERT an integer value into a table that exceeds the range of the underlying integer data type in the specified column. The easiest example of this is when you literally insert a too large value into the database.

Can integer be null Postgres?

An integer column can be null, but '' is an empty string not null. The right syntax for a null integer (or any other sql type) is null .

What is the difference between numeric and integer in PostgreSQL?

As opposed to INTEGER and BIGINT data types that can store only whole numbers, the DECIMAL and NUMERIC data types can store rational numbers. They can store 13,1072 digits before the decimal point and up to 16,383 digits after the decimal point.


2 Answers

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.

like image 200
Paul Rubel Avatar answered Sep 21 '22 16:09

Paul Rubel


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

like image 27
nsanders Avatar answered Sep 24 '22 16:09

nsanders