Python pandas.read\u sql读取未提交SQLAlchemy

Python pandas.read\u sql读取未提交SQLAlchemy,python,python-3.x,pandas,sqlalchemy,isolation-level,Python,Python 3.x,Pandas,Sqlalchemy,Isolation Level,我正在尝试使用pandas函数pd.read\u sql来读取在SQLAlchemy会话中创建、添加和刷新但未提交的记录。因此,我想在SQLAlchemy会话中创建一个对象,并在调用commit之前使用pandas查询它。使用pandas 0.22.0和SQLAlchemy 1.1.10 我尝试过在create\u engine上设置isolation\u level,以及将隔离级别设置为“READ UNCOMMITTED”的各种其他方法,但这似乎不起作用。下面是一个简单的例子: # Impor

我正在尝试使用pandas函数
pd.read\u sql
来读取在SQLAlchemy会话中创建、添加和刷新但未提交的记录。因此,我想在SQLAlchemy会话中创建一个对象,并在调用
commit
之前使用pandas查询它。使用pandas 0.22.0和SQLAlchemy 1.1.10

我尝试过在
create\u engine
上设置
isolation\u level
,以及将隔离级别设置为“READ UNCOMMITTED”的各种其他方法,但这似乎不起作用。下面是一个简单的例子:

# Import packages 

import pandas as pd
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine, Column, Integer, String
from sqlalchemy.orm import sessionmaker

# Set up an example ORM

Base = declarative_base()
class Record(Base):
    __tablename__ = 'records'
    id = Column(Integer, primary_key=True)
    foo = Column(String(255))

# Create a session and engine:

database='foobar'
user=''
password = ''
host = 'localhost'
port = '5432'

connection_string = f"postgresql+psycopg2://{user}:{password}@{host}:{port}/{database}"
engine = create_engine(connection_string, encoding = 'utf8', convert_unicode = True,
    isolation_level='READ_UNCOMMITTED'
)
session = sessionmaker()
session.configure(bind=engine)
db = session()

# Set up the example record:

Record.__table__.create(bind=engine)
record = Record(foo='bar')
db.add(record)
db.flush()

# Attempt to query:

records = pd.read_sql('select * from records', db.get_bind())
assert records.empty

我正在寻找一种解决方案,使上述代码在最后一行抛出一个
AssertionError
<代码>记录。空的当前的计算结果为真。

当然,我一发布到这里就知道了。对于后代:使用
db.connection()
而不是
db.get\u bind()

好奇的是,对于未提交的数据,您是否也可以在
pd.read\u sql
的con参数中使用引擎?@Parfait No,
db.get\u bind()
engine
相同
db.get_bind()是引擎
的计算结果为True。谢谢,我已经花了几个小时试图解决这个问题。这解决了我们使用sqlalchemy中的作用域会话的问题,其中需要先设置配置参数)。谢谢