title: query multiple SQLalchemy ORM
body: <p>I'm new in SQLalchemy I need to calculate multiple of some <code>price</code> in my one of the table. This is my tables: </p>
  
  <pre><code>class Order(DeclarativeBase):
      __tablename__ = 'order'
      id = Field(Integer, primary_key=True)
      products = relationship("OrderProduct", back_populates="order", lazy='dynamic')
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class OrderProduct(DeclarativeBase):
      __tablename__ = 'order_products'
      id = Field(Integer, primary_key=True)
      order_id = Column(Integer, ForeignKey('order.id'), nullable=False)
      order = relationship("Order", back_populates="products", protected=True)
      product_id = Column(Integer, ForeignKey('product.id'), nullable=False)
      product = relationship("Product", back_populates="order_product")
      quantity = Field(Integer, nullable=False)
  </code></pre>
  
  <p>and</p>
  
  <pre><code>class Product(DeclarativeBase):
      __tablename__ = 'product'
      id = Field(Integer, primary_key=True)
      price = Field(Integer)
      order_product = relationship("OrderProduct", back_populates="product", protected=True)
  </code></pre>
  
  <p>I want to multiple <code>price</code> with this situation <code>OrderProduct.quantity * Product.price</code> and <code>products</code> in <code>Order</code> table is an array of <code>Products</code></p>
  
  <p>I write <code>SQL</code> query like this and it works:</p>
  
  <pre><code>SELECT SUM(price*quantity) FROM product
  JOIN order_products ON product.id = order_products.product_id 
  JOIN order ON order_products.order_id = order.id;
  </code></pre>
  
  <p>I tried to make it in ORM like this but it takes me <code>Product</code> and I can calculate only <code>price</code> without multiple in <code>quantity</code>:</p>
  
  <pre><code>    result = 0
          for product in Product.query\
              .join(OrderProduct).filter(Product.id == OrderProduct.product_id)\
              .join(Order).filter(OrderProduct.order_id == self.id):
          result = product.price + result
      return result
  </code></pre>
  
  <p>I make this as <code>@hybrid_property</code> and its work well.</p>
  
  <p>I use a framework that name is <code>restfulpy</code>. it has ORM for sqlalchemy, <code>session</code> in this framework is <code>scoped_session</code>, but it gives me SQL query in debuging mode instead of executing the query like this :</p>
  
  <pre><code>sum_price = {Query}SELECT sum(product.price * order_products.quantity) AS sum_1 
  FROM product JOIN order_products ON product.id = order_products.product_id JOIN "order" ON "order".id = order_products.order_id 
  WHERE product.id = order_products.product_id AND order_products.order_id = :order_id_1
  </code></pre>
  
  <p>Well, Can anyone help me out this problem?</p>
  
  <p>with regards</p>