3

I am trying to use the pandas function pd.read_sql to read records that have been created, added, and flushed in a SQLAlchemy session, but not committed. So I want to create an object in a SQLAlchemy session and query it with pandas before calling commit. Using pandas 0.22.0 and SQLAlchemy 1.1.10.

I have tried setting the isolation_level on create_engine, and various other ways of setting the isolation level to 'READ UNCOMMITTED', but this does not seem to work. Minimal example below:

# 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

I am looking for a solution that will cause the above code to throw an AssertionError on the last line. records.empty currently evaluates to true.

1 Answer 1

6

And of course I figure it out as soon as I post here. For posterity: use db.connection() instead of db.get_bind().

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

4 Comments

Curious, can you also use engine in con argument of pd.read_sql for uncommitted data?
@Parfait No, db.get_bind() is the same as engine. db.get_bind() is engine evaluates to True.
THANK YOU, I've been trying to figure this out for hours.
This solved our issue where we are using a scoped session from sqlalchemy where a configuration parameter needed to be set first). Thanks!!!

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.