Skip to content

Instantly share code, notes, and snippets.

@kevinlondon
Last active January 2, 2021 06:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kevinlondon/35a1d30e36d4f4ee47ec8c9975871829 to your computer and use it in GitHub Desktop.
Save kevinlondon/35a1d30e36d4f4ee47ec8c9975871829 to your computer and use it in GitHub Desktop.
from sqlalchemy.orm import declarative_base, scoped_session, sessionmaker
from sqlalchemy.ext.declarative.base import _declarative_constructor
from sqlalchemy import Column, Integer, ForeignKey, create_engine
engine = create_engine(# engine init code here)
session = scoped_session(sessionmaker(bind=engine))
class CustomBase(object):
def __init__(self, **kwargs):
_declarative_constructor(self, **kwargs)
session.add(self)
Entity = declarative_base(cls=CustomBase, constructor=CustomBase.__init__)
class Item(Entity):
id = Column(Integer, primary_key=True)
type_id = Column(ForeignKey('item_type.id'), Integer)
class ItemType(Entity):
id = Column(Integer, primary_key=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment