from sqlalchemy import Column, Integer, String, Text, DateTime, Boolean, ForeignKey, Numeric from sqlalchemy.sql import func from sqlalchemy.orm import relationship from database import Base class TemplateDocument(Base): __tablename__ = "documents" __table_args__ = {"schema": "templates"} pk_document_id = Column(Integer, primary_key=True, index=True) document_name = Column(String(500)) document_type = Column(String(100)) file_name = Column(String(500)) file_hash = Column(String(500), index=True) page_count = Column(Integer) status = Column(String(50)) created_at = Column(DateTime, server_default=func.now()) updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now()) # Relationships layouts = relationship("DocumentLayout", back_populates="document", cascade="all, delete") class DocumentLayout(Base): __tablename__ = "document_layout" __table_args__ = {"schema": "templates"} pk_document_data_id = Column(Integer, primary_key=True, index=True) fk_document_id = Column(Integer, ForeignKey("templates.documents.pk_document_id", ondelete="CASCADE"), nullable=False, index=True) page_no = Column(Integer, nullable=False, index=True) text_value = Column(Text) block_type = Column(String(100)) parent_block_id = Column(Integer, ForeignKey("templates.document_layout.pk_document_data_id", ondelete="SET NULL")) x_coordinate = Column(Numeric) y_coordinate = Column(Numeric) width = Column(Numeric) height = Column(Numeric) confidence = Column(Numeric) sequence_no = Column(Integer) layout_path = Column(Text) created_at = Column(DateTime, server_default=func.now()) document = relationship("TemplateDocument", back_populates="layouts") parent = relationship("DocumentLayout", remote_side=[pk_document_data_id]) mappings = relationship("TemplateFieldMapping", back_populates="document_layout", cascade="all, delete") class Template(Base): __tablename__ = "templates" __table_args__ = {"schema": "templates"} pk_template_id = Column(Integer, primary_key=True, index=True) template_name = Column(String(255), nullable=False, index=True) template_fingerprint = Column(Text) active_flag = Column(Boolean, default=True) created_at = Column(DateTime, server_default=func.now()) updated_at = Column(DateTime, server_default=func.now(), onupdate=func.now()) fields = relationship("TemplateField", back_populates="template", cascade="all, delete") mappings = relationship("TemplateFieldMapping", back_populates="template", cascade="all, delete") class TemplateField(Base): __tablename__ = "template_fields" __table_args__ = {"schema": "templates"} pk_template_field_id = Column(Integer, primary_key=True, index=True) fk_template_id = Column(Integer, ForeignKey("templates.templates.pk_template_id", ondelete="CASCADE"), nullable=False, index=True) field_label = Column(String(255), nullable=False) field_type = Column(String(100), nullable=False) display_order = Column(Integer) required_flag = Column(Boolean, default=False) created_at = Column(DateTime, server_default=func.now()) template = relationship("Template", back_populates="fields") mappings = relationship("TemplateFieldMapping", back_populates="field", cascade="all, delete") class TemplateFieldMapping(Base): __tablename__ = "template_fields_mapping" __table_args__ = {"schema": "templates"} pk_mapping_id = Column(Integer, primary_key=True, index=True) fk_template_id = Column(Integer, ForeignKey("templates.templates.pk_template_id", ondelete="CASCADE"), nullable=False, index=True) fk_template_field_id = Column(Integer, ForeignKey("templates.template_fields.pk_template_field_id", ondelete="CASCADE"), nullable=False, index=True) fk_document_data_id = Column(Integer, ForeignKey("templates.document_layout.pk_document_data_id", ondelete="SET NULL")) page_no = Column(Integer) x_coordinate = Column(Numeric) y_coordinate = Column(Numeric) width = Column(Numeric) height = Column(Numeric) mapping_confidence = Column(Numeric) layout_path = Column(Text) created_at = Column(DateTime, server_default=func.now()) template = relationship("Template", back_populates="mappings") field = relationship("TemplateField", back_populates="mappings") document_layout = relationship("DocumentLayout", back_populates="mappings") class TemplateRecognitionHistory(Base): __tablename__ = "template_recognition_history" __table_args__ = {"schema": "templates"} pk_history_id = Column(Integer, primary_key=True, index=True) fk_template_id = Column(Integer, ForeignKey("templates.templates.pk_template_id", ondelete="CASCADE"), nullable=False) fk_document_id = Column(Integer, ForeignKey("templates.documents.pk_document_id", ondelete="CASCADE"), nullable=False, index=True) recognition_score = Column(Numeric) matched_flag = Column(Boolean) created_at = Column(DateTime, server_default=func.now())