diff --git a/models/deal.py b/models/deal.py index 03a0261..0704829 100644 --- a/models/deal.py +++ b/models/deal.py @@ -33,7 +33,7 @@ class Deal(BaseModel): is_completed = Column(Boolean, nullable=False, server_default='0', default=False, comment='Завершена') services = relationship('DealService', back_populates='deal', cascade="all, delete-orphan") - product_services = relationship('DealProductService', back_populates='deal', cascade="all, delete-orphan") + # product_services = relationship('DealProductService', back_populates='deal', cascade="all, delete-orphan") products = relationship('DealProduct', back_populates='deal', cascade="all, delete-orphan") diff --git a/models/secondary.py b/models/secondary.py index c12bfcb..222d19a 100644 --- a/models/secondary.py +++ b/models/secondary.py @@ -1,5 +1,5 @@ -from sqlalchemy import Table, Column, Integer, ForeignKey, Boolean -from sqlalchemy.orm import relationship +from sqlalchemy import Table, Column, Integer, ForeignKey, Boolean, and_, ForeignKeyConstraint +from sqlalchemy.orm import relationship, foreign, remote from models.base import metadata, BaseModel @@ -19,40 +19,35 @@ class DealService(BaseModel): price = Column(Integer, nullable=False, comment='Цена услуги') -class DealProduct(BaseModel): - __tablename__ = 'deal_products' - deal_id = Column(Integer, - ForeignKey('deals.id'), - nullable=False, - comment='ID Сделки', - primary_key=True) - deal = relationship('Deal', back_populates='products') - - product_id = Column(Integer, ForeignKey('products.id'), nullable=False, comment='ID Продукта', primary_key=True) - product = relationship('Product') - - quantity = Column(Integer, nullable=False, comment='Кол-во продукта') - - class DealProductService(BaseModel): __tablename__ = 'deal_product_services' - deal_id = Column(Integer, - ForeignKey('deals.id'), - nullable=False, - comment='ID Сделки', - primary_key=True) - deal = relationship('Deal', back_populates='product_services') - - product_id = Column(Integer, ForeignKey('products.id'), nullable=False, comment='ID Продукта', primary_key=True) - product = relationship('Product') - - service_id = Column(Integer, ForeignKey('services.id'), nullable=False, comment='ID Услуги', primary_key=True) - service = relationship('Service') - - quantity = Column(Integer, nullable=False, comment='Кол-во продукта') + 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='Цена услуги') - link_to_product_quantity = Column(Boolean, 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( diff --git a/schemas/deal.py b/schemas/deal.py index f1be7f6..df152ab 100644 --- a/schemas/deal.py +++ b/schemas/deal.py @@ -35,7 +35,6 @@ class DealServiceSchema(CustomModelCamel): class DealProductServiceSchema(CustomModelCamel): service: ServiceSchema - quantity: int price: int @@ -134,8 +133,7 @@ class DealUpdateProductQuantityRequest(CustomModelCamel): class DealAddProductRequest(CustomModelCamel): deal_id: int - product_id: int - quantity: int + product: DealProductSchema class DealDeleteProductRequest(CustomModelCamel): diff --git a/schemas/product.py b/schemas/product.py index 10c8193..41fdfb6 100644 --- a/schemas/product.py +++ b/schemas/product.py @@ -6,9 +6,7 @@ from models import ProductBarcode # region Entities - -class ProductSchema(CustomModelCamel): - id: int +class BaseProductSchema(CustomModelCamel): name: str article: str client_id: int @@ -29,15 +27,15 @@ class ProductSchema(CustomModelCamel): return v +class ProductSchema(BaseProductSchema): + id: int + + # endregion # region Requests -class ProductCreateRequest(CustomModelCamel): - name: str - article: str - client_id: int - barcodes: List[str] - barcode_template: BarcodeTemplateSchema | None = None +class ProductCreateRequest(BaseProductSchema): + pass class ProductDeleteRequest(CustomModelCamel): diff --git a/services/product.py b/services/product.py index 05963c2..edf1b09 100644 --- a/services/product.py +++ b/services/product.py @@ -39,6 +39,7 @@ class ProductService(BaseService): barcode=barcode) self.session.add(product_barcode) await self.session.flush() + await self.session.commit() return ProductCreateResponse(ok=True, message='Товар успешно создан', product_id=product.id)