from sqlalchemy import Column, BigInteger, String, Text, Integer, ForeignKey from sqlalchemy.dialects.postgresql import JSONB from sqlalchemy.orm import relationship from models.base import AuditBase class Subject(AuditBase): name = Column(String(45), nullable=False) modules = relationship("Module", back_populates="subject") library_files = relationship("LibraryFile", back_populates="subject") records = relationship("Record", back_populates="subject") mistakes = relationship("Mistake", back_populates="subject") class Module(AuditBase): subject_id = Column(BigInteger, ForeignKey("subject.id", ondelete="RESTRICT"), nullable=False, index=True) name = Column(String(45), nullable=False) subject = relationship("Subject", back_populates="modules") library_files = relationship("LibraryFile", back_populates="module") class LibraryFile(AuditBase): subject_id = Column(BigInteger, ForeignKey("subject.id", ondelete="RESTRICT"), nullable=False, index=True) module_id = Column(BigInteger, ForeignKey("module.id", ondelete="RESTRICT"), nullable=False, index=True) name = Column(String(45), nullable=False) type = Column(String(45), nullable=False) size = Column(Integer, nullable=False) content = Column(Text, nullable=False) subject = relationship("Subject", back_populates="library_files") module = relationship("Module", back_populates="library_files") class Record(AuditBase): type = Column(String(45), nullable=False) subject_id = Column(BigInteger, ForeignKey("subject.id", ondelete="RESTRICT"), nullable=False, index=True) total_count = Column(Integer, nullable=False) correct_count = Column(Integer, nullable=False) wrong_count = Column(Integer, nullable=False) subject = relationship("Subject", back_populates="records") class Mistake(AuditBase): subject_id = Column(BigInteger, ForeignKey("subject.id", ondelete="RESTRICT"), nullable=False, index=True) type = Column(String(45), nullable=False) question = Column(Text, nullable=False) options = Column(JSONB, nullable=False) answer = Column(JSONB, nullable=False) subject = relationship("Subject", back_populates="mistakes")