Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python sqlalchemy.exc.DataError:(psycopg2.DataError)整数超出范围_Python_Postgresql_Sqlalchemy - Fatal编程技术网

Python sqlalchemy.exc.DataError:(psycopg2.DataError)整数超出范围

Python sqlalchemy.exc.DataError:(psycopg2.DataError)整数超出范围,python,postgresql,sqlalchemy,Python,Postgresql,Sqlalchemy,我正试图通过SQLAchemy ORM将数据保存到postgresql中。我遇到以下错误: sqlalchemy.exc.DataError:(psycopg2.DataError)整数超出范围 我指出了出问题的地方。我有一个大号码,是2468432255.0。如果我改成更小的数字,比如468432255.0,那么它就可以工作了 让我困惑的是:我将列定义为volume=column(Numeric)。据我所知,Numeric应该能够处理这个大数字。此外,我还尝试了其他数据类型,如BigInt等。

我正试图通过SQLAchemy ORM将数据保存到postgresql中。我遇到以下错误:

sqlalchemy.exc.DataError:(psycopg2.DataError)整数超出范围

我指出了出问题的地方。我有一个大号码,是2468432255.0。如果我改成更小的数字,比如468432255.0,那么它就可以工作了

让我困惑的是:我将列定义为volume=column(Numeric)。据我所知,Numeric应该能够处理这个大数字。此外,我还尝试了其他数据类型,如BigInt等。。。他们都给了我同样的错误

有什么想法吗

谢谢,
Chengjun

您可以在本地代码的SQlAlchemy模式中定义任何您想要的内容, 这并不意味着你要插入数据的数据库会尊重它

SQLAlchemy中定义的模式由代码本身强制执行

而DB有自己的模式,当您尝试插入/删除等时,该模式也会强制执行。。(有自己的约束等等)SQLAlchemy对此一无所知(除非您声明)

在我看来,您可以简单地自动生成SQLAlchemy模式——它将从DB模式中获取DB列名和类型

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))
创建
SomeTable
对象后,只需

SomeTable.colname

colname
是数据库中当前存在的列

我收到了相同的错误-

下面是一些测试代码-使用对象接口时可以,但使用普通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

我不知道它为什么会爆炸——在某个时刻,psycopg2一定在尝试将bigint转换为int。

也许您可以发布一个代码示例,为用户重现这个问题。