38 lines
1.4 KiB
Python
38 lines
1.4 KiB
Python
from sqlalchemy import Column, Integer, ForeignKey
|
|
from sqlalchemy.orm import mapped_column, Mapped, relationship
|
|
|
|
from database import Base
|
|
|
|
|
|
class MPProfile(Base):
|
|
__tablename__ = 'mp_profile'
|
|
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
external_id: Mapped[int] = mapped_column(unique=True)
|
|
chats: Mapped[list['MPChat']] = relationship('MPChat', secondary='mp_chat_user')
|
|
|
|
|
|
class MPChat(Base):
|
|
__tablename__ = 'mp_chat'
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
name: Mapped[str | None]
|
|
admin_id: Mapped[int] = mapped_column(ForeignKey('mp_profile.id'))
|
|
admin: Mapped[MPProfile] = relationship()
|
|
users: Mapped[list[MPProfile]] = relationship('MPProfile', secondary='mp_chat_user')
|
|
|
|
|
|
class MPMessage(Base):
|
|
__tablename__ = 'mp_message'
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
sender_id: Mapped[int] = mapped_column(ForeignKey('mp_profile.id'))
|
|
chat_id: Mapped[int] = mapped_column(ForeignKey('mp_chat.id'))
|
|
content: Mapped[str]
|
|
chat: Mapped[MPChat] = relationship()
|
|
sender: Mapped[MPProfile] = relationship()
|
|
|
|
|
|
class MPChatUser(Base):
|
|
__tablename__ = 'mp_chat_user'
|
|
id: Mapped[int] = mapped_column(primary_key=True, autoincrement=True)
|
|
user_id: Mapped[int] = mapped_column(ForeignKey('mp_profile.id', ondelete='CASCADE'))
|
|
chat_id: Mapped[int] = mapped_column(ForeignKey('mp_chat.id', ondelete='CASCADE')) |