sqlalchemy.exc.DataError: (psycopg2.DataError) integer out of range

2,310 views
Skip to first unread message

Chengjun JIN

unread,
Jul 13, 2015, 7:02:45 PM7/13/15
to sqlal...@googlegroups.com
Hi all,

I am trying to save data into postgresql via sqlchemy ORM. I encountered an error below: sqlalchemy.exc.DataError: (psycopg2.DataError) integer out of range

I pinpoint the place where it goes wrong. I have a large number which is 2468432255.0. If I change to smaller number like 468432255.0, then it works.

The thing confused me is that: I define the column as volume = Column(Numeric). As far as I understand, Numeric should be able to handle this large number.  Additionally, I tried other data type like BigInt etc... They all gave me the same error.

Any idea?

Thanks,
Chengjun


Mike Bayer

unread,
Jul 13, 2015, 7:47:08 PM7/13/15
to sqlal...@googlegroups.com
I can't reproduce that.  Here's a test:

from sqlalchemy import create_engine, Numeric, MetaData, Table, Column

m = MetaData()
t = Table('x', m, Column('data', Numeric))
e = create_engine("postgresql://scott:tiger@localhost/test", echo=True)
c = e.connect()
tr = c.begin()
m.create_all(c)

c.execute(
    t.insert(),
    {"data": 2468432255.0}
)



What is the actual NUMERIC type in the schema?  If you make a NUMERIC type with too low of a precision/scale, the number will be rejected.  example:

from sqlalchemy import create_engine, Numeric, MetaData, Table, Column

m = MetaData()
t = Table('x', m, Column('data', Numeric(6, 2)))
e = create_engine("postgresql://scott:tiger@localhost/test", echo=True)
c = e.connect()
tr = c.begin()
m.create_all(c)

c.execute(
    t.insert(),
    {"data": 2468432255.0}
)


output:

sqlalchemy.exc.DataError: (psycopg2.DataError) numeric field overflow
DETAIL:  A field with precision 6, scale 2 must round to an absolute value less than 10^4.
 [SQL: 'INSERT INTO x (data) VALUES (%(data)s)'] [parameters: {'data': 2468432255.0}]



Please adapt the test above to illustrate how you're getting your error exactly. 





Any idea?

Thanks,
Chengjun


--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.
To post to this group, send email to sqlal...@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Chengjun JIN

unread,
Jul 14, 2015, 5:30:44 AM7/14/15
to sqlal...@googlegroups.com
Hi Michael,

Thanks for your time.

I have figured out the problem. My error is not related to the data type, as you pointed out. It is related to the definition of the TABLE column repeatedly and inconsistently (meaning: initially I define the column as Integer in the database, then I changed to Numeric but it is not changed, due to my bad coding).

Regards,
Chengjun
Reply all
Reply to author
Forward
0 new messages