Skip to content

Instantly share code, notes, and snippets.

@metatoaster
Last active May 3, 2023 08:09
Show Gist options
  • Save metatoaster/3199779d612b6deed0046107e9d7ad5e to your computer and use it in GitHub Desktop.
Save metatoaster/3199779d612b6deed0046107e9d7ad5e to your computer and use it in GitHub Desktop.
StackOverflow 76160846
# pip install SQLAlchemy==2.0.12 psycopg2==2.9.6
import random
from sqlalchemy import Column, Integer, String
from sqlalchemy import create_engine
from sqlalchemy import select
from sqlalchemy.orm import declarative_base, sessionmaker, scoped_session
Base = declarative_base()
class Entity(Base):
__tablename__ = 'entity'
id = Column(Integer, primary_key=True)
field_a = Column(String(255))
def main():
engine = create_engine('postgresql://postgres@localhost/postgres')
Base.metadata.create_all(engine)
Session = scoped_session(sessionmaker(bind=engine))
session = Session()
session_replica = Session()
entity = session.query(Entity).first()
entity.field_a = str(random.random())
session.add(entity)
session.commit()
entity_rr = session_replica.query(Entity).filter(Entity.id == entity.id).one()
assert entity_rr.field_a == entity.field_a
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment