从Python

时间:2017-11-27 11:28:16

标签: python flask sqlalchemy

我是Python新手,目前正在尝试创建一个Web表单来编辑客户数据。用户选择客户并获得链接到客户的所有DSL产品。我现在正在尝试的是为客户提供最大的下游可能性。所以当客户拿到DSL1,DSL3和DSL3时,他的MaxDownstream就是550.抱歉我的英语技能很差。

这是我桌子的结构..

Customer_has_product:
Customer_idCustomer | Product_idProduct
----------------------------
1                 |       1
1                 |       3
1                 |       4
2                 |       5
3                 |       3

Customer:
idCustomer | MaxDownstream
----------------------------
1         |       
2         |       
3         |       


Product:
idProduct | Name            | downstream
-------------------------------------------------
1         | DSL1            | 50
2         | DSL2            | 100
3         | DSL3            | 550
4         | DSL4            | 400
5         | DSL5            | 1000

到目前为止我已经获得了代码:

db_session = Session(db_engine)
customer_object = db_session.query(Customer).filter_by(
    idCustomer=productform.Customer.data.idCustomer
).first()
productlist = request.form.getlist("DSLPRODUCTS_PRIVATE")
oldproducts = db_session.query(Customer_has_product.Product_idProduct).filter_by(
    Customer_idCustomer=customer_object.idCustomer)
id_list_delete = list(set([r for r, in oldproducts]) - set(productlist))
for delid in id_list_delete:
    db_session.query(Customer_has_product).filter_by(Customer_idCustomer=customer_object.idCustomer,
                                                    Product_idProduct=delid).delete()
db_session.commit()
for product in productlist:
    if db_session.query(Customer_has_product).filter_by(
        Customer_idCustomer=customer_object.idCustomer,
        Product_idProduct=product
    ).first() is not None:
        continue
    else:
        product_link_to_add = Customer_has_product(
            Customer_idCustomer=productform.Customer.data.idCustomer,
            Product_idProduct=product
        )
            db_session.add(product_link_to_add)
            db_session.commit()

1 个答案:

答案 0 :(得分:0)

你想要做的是JOIN the tables彼此。所有关系数据库引擎都支持联接,SQLAlchemy也是如此。

那么你如何在SQLAlchemy中做到这一点?

你有两个选择,真的。一种是使用SQLAlchemy的ORM的Query构建器,另一种是使用SQLAlchemy Core(在其上构建ORM)。我真的更喜欢后者,因为它更直接地映射到SELECT语句,但我将展示两者。

使用SQLAlchemy Core

如何在Core中join记录here。第一个参数是JOIN到的表,第二个参数是JOIN条件。

from sqlalchemy import select, func

query = select(
    [
        Customer.idCustomer,
        func.max(Product.downstream),
    ]
).select_from(
    Customer.__table__
    .join(Customer_has_product.__table__,
          Customer_has_product.Customer_idCustomer ==
              Customer.idCustomer)
    .join(Product.__table__,
          Product.idProduct == Customer_has_product.Product_idProduct)
).group_by(
    Customer.idCustomer
)

# Now we can execute the built query on the database.
result = db_session.execute(query).fetchall()
print(result)  # Should now give you the correct result.

使用SQLAlchemy ORM

为了简化这一点,最好声明一些[relationship s on your models][2]. join is documented [here][2]. First argument to join`是要加入的模型,第二个参数是JOIN条件。

如果没有关系,你就必须这样做。

result = (db_session
    .query(Customer.idCustomer, func.max(Product.downstream))
    .join(Customer_has_product,
          Customer_has_product.Customer_idCustomer ==
              Customer.idCustomer)
    .join(Product,
          Product.idProduct == Customer_has_product.Product_idProduct)
    .group_by(Customer.idCustomer)
).all()
print(result)

这应该足以让我们了解如何做到这一点。