SqlAlchemy 产品 table 加入股票 table 总和

SqlAchemy products table join to stock table with sum

我如何加入这些 table 以便我可以列出所有产品及其来自库存 table 的总库存数量,我还想加入类别 table 以产品 table 所以我可以获得类别名称。

class Stock(Base):
    __tablename__ = 'stock'

    id = Column(Integer, primary_key=True)
    stock = Column(Integer, nullable=False)
    transaction_type = Column(String(45), nullable=False)
    status = Column(String(45), nullable=False)
    product_id = Column(ForeignKey('product.id'), nullable=False, index=True)
    created_at = Column(DateTime, nullable=False)
    price = Column(Integer)
    sale_receipt_id = Column(ForeignKey('sale_receipt.id'), index=True)

    product = relationship('Product')
    sale_receipt = relationship('SaleReceipt')

class Product(Base):
    __tablename__ = 'product'

    id = Column(Integer, primary_key=True)
    name = Column(String(45), nullable=False)
    description = Column(String(45), nullable=False)
    barcode = Column(BigInteger, nullable=False)
    sku = Column(String(45), nullable=False)
    company_id = Column(ForeignKey('company.id'), nullable=False, index=True)
    cost_price_id = Column(ForeignKey('cost_price.id'), nullable=False, index=True)
    sell_price_id = Column(ForeignKey('sell_price.id'), nullable=False, index=True)
    supplier_id = Column('supplier.id', ForeignKey('supplier.id'), nullable=False, index=True)
    category_id = Column(ForeignKey('category.id'), nullable=False, index=True)
    variant_id = Column(ForeignKey('variant.id'), nullable=False, index=True)
    size_id = Column(ForeignKey('size.id'), nullable=False, index=True)
    status = Column(String(45), nullable=False)
    image = Column(JSON)
    reorder_threshold = Column(String(45))
    tags = Column(String(200))

    category = relationship('Category')
    company = relationship('Company')
    cost_price = relationship('CostPrice')
    sell_price = relationship('SellPrice')
    size = relationship('Size')
    supplier = relationship('Supplier')
    variant = relationship('Variant')

class Category(Base):
    __tablename__ = 'category'

    id = Column(Integer, primary_key=True)
    name = Column(String(45), nullable=False)
    company_id = Column(ForeignKey('company.id'), nullable=False, index=True)
    status = Column(String(45), nullable=False)

    company = relationship('Company')

我可以对库存求和,但我不知道如何将产品加入其中。

rds_session.query(Stock.product_id, func.sum(Stock.stock).label("stockTotal")).join(Product).group_by(Stock.product_id).all()

这给了我这样的元组列表

[(<functions.database.db.Product object at 0x7f863ce650b8>, Decimal('3'))]

我想要在单个实体中汇总库存和产品。 抱歉英语不好让我知道如果有什么不清楚

---- 已解决 ---- 我能够通过这个查询做到这一点:

x_add = rds_sesssion.query(Stock.product_id, func.sum(func.IF(Stock.transaction_type == 'add', Stock.stock, -Stock.stock)).label('totalStock')).group_by(Stock.product_id).subquery()
results = rds_sesssion.query(Product, x_add.c.totalStock).join(x_add, Product.id == x_add.c.product_id).all()

我不确定您是如何通过上面给定的查询获得二元组的。一种解决方案是将计数总和查询放入子查询中,并从 Product 中加入该子查询。我不确定你所说的“单一实体”是什么意思,因为你仍然得到一个二元组列表。您是否希望将计算的 属性 附加到 class ?该类别应该在 product.category 上可用。您可能希望在第二个查询中使用 .options(joinedload(Product.category)) 来优化加载类别。


# Create query to get the count and use .subquery().
stock_subquery = rds_session.query(Stock.product_id, func.sum(Stock.stock).label("stockTotal")).group_by(Stock.product_id).subquery()

# Join Product with subquery
# note that columns in subquery are only accessible with c and
# that we use the label created above.
# This should return a list of 2-tuples of the for:
# [(Product, stockTotal)...]
results = rds_session.query(Product, stock_subquery.c.stockTotal).join(stock_subquery, Product.id == stock_subquery.c.product_id).all()

for product, stock_total in results:
    print (product, product.category, stock_total)