from sqlalchemy import Column, String, Integer, DateTime, JSON, ForeignKey, Boolean from sqlalchemy.sql import func from sqlalchemy.orm import relationship, declared_attr import uuid from .database import Base class Unloading(Base): __tablename__ = "unloading" guid = Column(String(36), primary_key=True, default=lambda: str(uuid.uuid4()).upper()) name = Column(String(500), nullable=False) unloading_time_seconds = Column(Integer, nullable=False) warnings = Column(JSON, default={}) row_count = Column(Integer, nullable=False) created_at = Column(DateTime(timezone=True), server_default=func.now()) file_count = Column(Integer, nullable=False) computer_name = Column(String(100), nullable=True) # ДОБАВЬТЕ ЭТОТ ВНЕШНИЙ КЛЮЧ product_guid = Column(String(36), ForeignKey("product.guid"), unique=True) # Исправленная связь product = relationship("Product", back_populates="unloading", uselist=False) class Product(Base): __tablename__ = "product" guid = Column(String(36), primary_key=True, index=True) name = Column(String(500), nullable=False) # Связь остается без изменений unloading = relationship("Unloading", back_populates="product", uselist=False) unload_checks = relationship("UnloadCheck", back_populates="product", cascade="all, delete-orphan") test_loads = relationship("TestLoad", back_populates="product", cascade="all, delete-orphan") test_checks = relationship("TestCheck", back_populates="product", cascade="all, delete-orphan") prod_transfers = relationship("ProdTransfer", back_populates="product", cascade="all, delete-orphan") class BaseStage(Base): __abstract__ = True id = Column(Integer, primary_key=True, index=True) fio = Column(String(200), nullable=False) date = Column(DateTime(timezone=True), server_default=func.now()) is_completed = Column(Boolean, default=False) @declared_attr def product_guid(cls): return Column(String(36), ForeignKey("product.guid")) class UnloadCheck(BaseStage): __tablename__ = "unload_check" product = relationship("Product", back_populates="unload_checks") class TestLoad(BaseStage): __tablename__ = "test_load" product = relationship("Product", back_populates="test_loads") class TestCheck(BaseStage): __tablename__ = "test_check" product = relationship("Product", back_populates="test_checks") class ProdTransfer(BaseStage): __tablename__ = "prod_transfer" product = relationship("Product", back_populates="prod_transfers")