Skip to content

Instantly share code, notes, and snippets.

@daaniam
Created February 15, 2023 16:11
Show Gist options
  • Save daaniam/68904ae53dbb7752a4f25e313d369ea9 to your computer and use it in GitHub Desktop.
Save daaniam/68904ae53dbb7752a4f25e313d369ea9 to your computer and use it in GitHub Desktop.
SQL Alchemy custom Base with Mixins
from sqlalchemy import TIMESTAMP, UUID, Column
from sqlalchemy.orm import declarative_mixin
from sqlalchemy.orm import declarative_base
# MIXINS:
@declarative_mixin
class IDMixin:
id = Column(UUID, primary_key=True)
@declarative_mixin
class TimestampMixin:
created_at = Column(TIMESTAMP(timezone=True))
updated_at = Column(TIMESTAMP(timezone=True))
# Inhertit and use cls as factory:
class Base(IDMixin, TimestampMixin):
...
Base = declarative_base(cls=Base)
__all__=["Base"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment