mirror of
https://gitlab.com/MoonTestUse1/AdministrationItDepartmens.git
synced 2025-08-14 00:25:46 +02:00
46 lines
2.0 KiB
Python
46 lines
2.0 KiB
Python
from sqlalchemy import Column, Integer, String, ForeignKey, DateTime, Boolean, Text
|
|
from sqlalchemy.sql import func
|
|
from sqlalchemy.orm import relationship
|
|
|
|
from app.database import Base
|
|
|
|
class Chat(Base):
|
|
__tablename__ = "chats"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
employee_id = Column(Integer, ForeignKey("users.id"), nullable=False, unique=True)
|
|
admin_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
|
|
# Отношения
|
|
employee = relationship("User", foreign_keys=[employee_id], back_populates="employee_chats")
|
|
admin = relationship("User", foreign_keys=[admin_id], back_populates="admin_chats")
|
|
messages = relationship("Message", back_populates="chat", cascade="all, delete-orphan")
|
|
|
|
class Message(Base):
|
|
__tablename__ = "messages"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
chat_id = Column(Integer, ForeignKey("chats.id", ondelete="CASCADE"), nullable=False)
|
|
sender_id = Column(Integer, ForeignKey("users.id"), nullable=False)
|
|
content = Column(Text, nullable=False)
|
|
is_read = Column(Boolean, default=False)
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
|
|
# Отношения
|
|
chat = relationship("Chat", back_populates="messages")
|
|
sender = relationship("User", back_populates="sent_messages")
|
|
files = relationship("ChatFile", back_populates="message", cascade="all, delete-orphan")
|
|
|
|
class ChatFile(Base):
|
|
__tablename__ = "chat_files"
|
|
|
|
id = Column(Integer, primary_key=True, index=True)
|
|
message_id = Column(Integer, ForeignKey("messages.id", ondelete="CASCADE"), nullable=False)
|
|
file_name = Column(String(255), nullable=False)
|
|
file_path = Column(String(255), nullable=False)
|
|
file_size = Column(Integer, nullable=False)
|
|
created_at = Column(DateTime(timezone=True), server_default=func.now())
|
|
|
|
# Отношения
|
|
message = relationship("Message", back_populates="files") |