Skip to content

Instantly share code, notes, and snippets.

Created June 16, 2020 14:39
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
from sqlalchemy import create_engine, Column, String
from sqlalchemy.ext.declarative import declarative_base, declared_attr
from sqlalchemy.orm import sessionmaker, scoped_session
engine = create_engine(
db_session = scoped_session(
sessionmaker(autocommit=False, bind=engine)
class Base:
def __tablename__(cls):
return (
"".join("_%s" % c if c.isupper() else c for c in cls.__name__)
Base = declarative_base(cls=Base)
class MultiPK(Base):
code = Column(String(50), primary_key=True, autoincrement=False)
other = Column(String(50), primary_key=True, autoincrement=False)
data = Column(String(50))
db_session.add(MultiPK(code="a", other="b", data="pre"))
db_session = None
# At a later date, go and update it. I.e. this would happen in a different requres
db_session = scoped_session(
sessionmaker(autocommit=False, autoflush=False, bind=engine)
db_session.merge(MultiPK(code="a", other="b", data="post"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment