from sqlalchemy import Table, Column, Integer, ForeignKey, Boolean, and_, ForeignKeyConstraint from sqlalchemy.orm import relationship, foreign, remote from models.base import metadata, BaseModel class DealService(BaseModel): __tablename__ = 'deal_services' deal_id = Column(Integer, ForeignKey('deals.id'), nullable=False, comment='ID Сделки', primary_key=True) deal = relationship('Deal', back_populates='services') service_id = Column(Integer, ForeignKey('services.id'), nullable=False, comment='ID Услуги', primary_key=True) service = relationship('Service') quantity = Column(Integer, nullable=False, comment='Кол-во услуги') price = Column(Integer, nullable=False, comment='Цена услуги') class DealProductService(BaseModel): __tablename__ = 'deal_product_services' deal_id = Column(Integer, nullable=False, primary_key=True, comment='ID Сделки') product_id = Column(Integer, nullable=False, primary_key=True, comment='ID Продукта') service_id = Column(Integer, ForeignKey('services.id'), nullable=False, comment='ID Услуги') price = Column(Integer, nullable=False, comment='Цена услуги') __table_args__ = ( ForeignKeyConstraint( ['deal_id', 'product_id'], ['deal_products.deal_id', 'deal_products.product_id'] ), ) deal_product = relationship('DealProduct', back_populates='services') service = relationship('Service', lazy='joined') class DealProduct(BaseModel): __tablename__ = 'deal_products' deal_id = Column(Integer, ForeignKey('deals.id'), nullable=False, comment='ID Сделки', primary_key=True) product_id = Column(Integer, ForeignKey('products.id'), nullable=False, comment='ID Продукта', primary_key=True) quantity = Column(Integer, nullable=False, comment='Кол-во продукта') deal = relationship('Deal', back_populates='products') product = relationship('Product') services = relationship('DealProductService', back_populates='deal_product', lazy='joined', cascade="all, delete-orphan") barcode_template_attribute_link = Table( 'barcode_template_attribute_links', BaseModel.metadata, Column('barcode_template_id', ForeignKey('barcode_templates.id')), Column('attribute_id', ForeignKey('barcode_template_attributes.id')) )