使用 sqlalchemy 时出现溢出错误
OverflowError using sqlalchemy
使用 SQLAlchemy 连接到数据库时遇到问题。我已经使用这个包很长时间来连接到我们拥有的所有数据库。最近试图连接到一个新的数据库并得到以下错误:
from sqlalchemy import create_engine
engine = create_engine('postgresql://username:password@host/db')
engine.connect()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 1778, in connect
return self._connection_cls(self, **kwargs)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 60, in __init__
self.__connection = connection or engine.raw_connection()
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 1847, in raw_connection
return self.pool.unique_connection()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 280, in unique_connection
return _ConnectionFairy._checkout(self)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 644, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 440, in checkout
rec = pool._do_get()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 963, in _do_get
return self._create_connection()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 285, in _create_connection
return _ConnectionRecord(self)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 416, in __init__
exec_once(self.connection, self)
File "C:\Python27\lib\site-packages\sqlalchemy\event\attr.py", line 250, in exec_once
self(*args, **kw)
File "C:\Python27\lib\site-packages\sqlalchemy\event\attr.py", line 260, in __call__
fn(*args, **kw)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\strategies.py", line 157, in on_connect
do_on_connect(conn)
File "C:\Python27\lib\site-packages\sqlalchemy\dialects\postgresql\psycopg2.py", line 530, in on_connect
for fn in fns:
OverflowError: Python int too large to convert to C long
奇怪的是,对于某些数据库,我的连接工作得很好,但对于这个特定的数据库,我得到了 OverflowError。关于为什么会发生这种情况的任何想法?我们正在使用 postgresql
问题是由psycopg2中的hstore实现引起的。在连接时,psycopg2 尝试使用获取 hstore OID 的本机 hstore 实现。这些 OID 超出了 C 的长范围值,因此引发了错误。
您可以通过在 create_engine
中传递 use_native_hstore=False 来解决问题
engine = create_engine('postgresql://username:password@host/db', use_native_hstore=False)
使用 SQLAlchemy 连接到数据库时遇到问题。我已经使用这个包很长时间来连接到我们拥有的所有数据库。最近试图连接到一个新的数据库并得到以下错误:
from sqlalchemy import create_engine
engine = create_engine('postgresql://username:password@host/db')
engine.connect()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 1778, in connect
return self._connection_cls(self, **kwargs)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 60, in __init__
self.__connection = connection or engine.raw_connection()
File "C:\Python27\lib\site-packages\sqlalchemy\engine\base.py", line 1847, in raw_connection
return self.pool.unique_connection()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 280, in unique_connection
return _ConnectionFairy._checkout(self)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 644, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 440, in checkout
rec = pool._do_get()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 963, in _do_get
return self._create_connection()
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 285, in _create_connection
return _ConnectionRecord(self)
File "C:\Python27\lib\site-packages\sqlalchemy\pool.py", line 416, in __init__
exec_once(self.connection, self)
File "C:\Python27\lib\site-packages\sqlalchemy\event\attr.py", line 250, in exec_once
self(*args, **kw)
File "C:\Python27\lib\site-packages\sqlalchemy\event\attr.py", line 260, in __call__
fn(*args, **kw)
File "C:\Python27\lib\site-packages\sqlalchemy\engine\strategies.py", line 157, in on_connect
do_on_connect(conn)
File "C:\Python27\lib\site-packages\sqlalchemy\dialects\postgresql\psycopg2.py", line 530, in on_connect
for fn in fns:
OverflowError: Python int too large to convert to C long
奇怪的是,对于某些数据库,我的连接工作得很好,但对于这个特定的数据库,我得到了 OverflowError。关于为什么会发生这种情况的任何想法?我们正在使用 postgresql
问题是由psycopg2中的hstore实现引起的。在连接时,psycopg2 尝试使用获取 hstore OID 的本机 hstore 实现。这些 OID 超出了 C 的长范围值,因此引发了错误。
您可以通过在 create_engine
中传递 use_native_hstore=False 来解决问题engine = create_engine('postgresql://username:password@host/db', use_native_hstore=False)