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,server_default='0', comment='Цена услуги') class DealProductService(BaseModel): __tablename__ = 'deal_product_services' deal_id = Column(Integer, primary_key=True, nullable=False, comment='ID Сделки') product_id = Column(Integer, primary_key=True, nullable=False, comment='ID Продукта') service_id = Column(Integer, ForeignKey('services.id'), primary_key=True, nullable=False, comment='ID Услуги') price = Column(Integer, nullable=False, comment='Цена услуги') deal_product = relationship('DealProduct', back_populates='services', primaryjoin="and_(DealProductService.deal_id == DealProduct.deal_id, " "DealProductService.product_id == DealProduct.product_id)", foreign_keys=[deal_id, product_id]) service = relationship('Service', foreign_keys=[service_id], lazy='joined') __table_args__ = ( ForeignKeyConstraint( ['deal_id', 'product_id'], ['deal_products.deal_id', 'deal_products.product_id'] ), ) class DealProduct(BaseModel): __tablename__ = 'deal_products' deal_id = Column(Integer, ForeignKey('deals.id'), primary_key=True, nullable=False, comment='ID Сделки') product_id = Column(Integer, ForeignKey('products.id'), primary_key=True, nullable=False, comment='ID Продукта') quantity = Column(Integer, nullable=False, comment='Кол-во продукта') deal = relationship('Deal', back_populates='products', foreign_keys=[deal_id]) product = relationship('Product', lazy='joined', foreign_keys=[product_id]) services = relationship('DealProductService', back_populates='deal_product', cascade="all, delete-orphan", primaryjoin="and_(DealProductService.deal_id == DealProduct.deal_id, " "DealProductService.product_id == DealProduct.product_id)", foreign_keys=[DealProductService.deal_id, DealProductService.product_id], lazy='joined' ) 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')) )