20 lines
815 B
Python
20 lines
815 B
Python
from datetime import datetime
|
|
from sqlalchemy import Column, Integer, DateTime
|
|
from sqlalchemy.ext.declarative import declared_attr
|
|
from app.db.session import Base
|
|
|
|
class TimestampMixin:
|
|
"""Mixin that adds created_at and updated_at columns to a table."""
|
|
created_at = Column(DateTime, default=datetime.utcnow, nullable=False)
|
|
updated_at = Column(DateTime, default=datetime.utcnow, onupdate=datetime.utcnow, nullable=False)
|
|
|
|
class TableNameMixin:
|
|
"""Mixin that sets the table name to the lowercase pluralized class name."""
|
|
@declared_attr
|
|
def __tablename__(cls):
|
|
return cls.__name__.lower() + 's'
|
|
|
|
class BaseModel(TimestampMixin, TableNameMixin, Base):
|
|
"""Base model for all models to inherit from."""
|
|
__abstract__ = True
|
|
id = Column(Integer, primary_key=True, index=True) |