9

I am trying to save data into postgresql via SQLAchemy ORM. I encountered an error below:

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

I pinpointed 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 defined 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

1
  • Maybe you could post a sample of your code that reproduces the problem for users. Commented Jul 13, 2015 at 23:37

2 Answers 2

2

You can define anything you want in the SQlAlchemy schema in your local code, It doesn't mean it will be honored by the DB you're inserting the data into.

The schema that is defined in SQLAlchemy is being enforced by the code itself.

While the DB has its own schema that is also enforcing it when you're trying to insert/delete etc .. ( has its own constraints etc .. ) that SQLAlchemy knows nothing about ( until you declare it ).

In my opinion you can simply generate the SQLAlchemy schema automatically - and it will take the DB column name & types from the DB schema.

from sqlalchemy import create_engine

class SomeTable(Base):
    """
    Class, that represents SomeTable
    """
    __tablename__ = "my_table_in_db"

    __table__ = Table(__tablename__, Base.metadata,
        autoload=True,
        autoload_with=create_engine(DB_URL))

And after you'll create a SomeTable object, you can access its columns simply by

SomeTable.colname

colname is a column that currently exists in the DB

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

Comments

-2

I was getting this same error -

Here's some test code - it's okay when using the object interface, but bombs using plain SQL:

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

dsn = 'postgres://postgres@localhost/testdb'

engine = create_engine(dsn)
metadata = MetaData(engine)

# create table
tbl = Table('test', metadata, Column('id', BigInteger))
tbl.drop(checkfirst=True)
tbl.create(checkfirst=True)

# use object chaining - works
cmd = tbl.insert().values(id=123456789012)
engine.execute(cmd)

# pass a dictionary of values - works
cmd = tbl.insert(values = dict(id=123456789013))
engine.execute(cmd)

# pass a list of values - works
cmd = tbl.insert(values = [123456789014])
engine.execute(cmd)

# use plain sql - bombs!
# error - sqlalchemy.exc.DataError: (psycopg2.DataError) integer out of range
# sql = "INSERT INTO test (id) VALUES (123456789015);"
# engine.execute(sql)

# show table contents
cmd = tbl.select(True)
result = engine.execute(cmd)
for row in result:
    print(row)
# (123456789012,)
# (123456789013,)
# (123456789014,)

# show table definition
for t in metadata.sorted_tables:
    print("Table name: ", t.name)
    for c in t.columns:
        print("  ", c.name, c.type)
# Table name:  test
#    id BIGINT

I don't know why it bombs - at some point psycopg2 must be trying to convert the bigint to an int.

Comments

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.