class BaseModelMixin: _model = None readable_field = "id" @declared_attr def __tablename__(self) -> str: return self.__name__.lower() # type: ignore id = db.Column(db.Integer, primary_key=True, autoincrement=True) created_at = db.Column(db.DateTime(timezone=True), default=db.func.now()) updated_at = db.Column( db.DateTime(timezone=True), default=db.func.now(), onupdate=db.func.now(), ) @classmethod def model(cls) -> Type[BaseModel]: if not cls._model: cls._model = sqlalchemy_to_pydantic( cls, config=PydanticConfig, exclude=["id"] ) return cls._model def __repr__(self) -> str: return f"<{self.__class__.__name__}: {getattr(self, self.readable_field)}>"
class Message(db.Model): __tablename__ = 'chat_messages' id = db.Column(db.Integer(), primary_key=True) text = db.Column(db.Unicode()) created_date = db.Column(db.DateTime()) room_id = db.Column(db.Integer(), db.ForeignKey('chat_rooms.id')) user_name = db.Column(db.String(), db.ForeignKey('users.username'))
class Room(db.Model): __tablename__ = 'chat_rooms' id = db.Column(db.Integer(), primary_key=True) name = db.Column(db.Unicode(30)) created_date = db.Column(db.DateTime())