Created
February 15, 2023 16:11
-
-
Save daaniam/68904ae53dbb7752a4f25e313d369ea9 to your computer and use it in GitHub Desktop.
SQL Alchemy custom Base with Mixins
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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