简体   繁体   English

SQL /peewee。 如何根据另一个表的条件更新记录?

[英]SQL /peewee. How do I update a record based on condition from another table?

So I am trying to update a store after an item is bought.因此,我正在尝试在购买商品后更新商店。 These are the models I'm working with.这些是我正在使用的模型。

class Cart():
    user = pw.ForeignKeyField(User, backref="cart_user", null=True)
    item = pw.ForeignKeyField(
        Item, backref="cart_item", null=True, on_delete='SET NULL')
    payment_status = pw.BooleanField(null=False, default=False)
    payment = pw.ForeignKeyField(Payment, backref="payment", null=True)
    amount = pw.IntegerField(null=False, default=1)

class Item():
    name = pw.CharField(null=False)
    product_type = pw.CharField(null=False)
    size = pw.CharField(null=False)
    color = pw.CharField(null=False, default=False)
    price = pw.IntegerField(null=False)
    image = pw.CharField(null=False)
    stock = pw.IntegerField(null=True)

After a buy, I would like the value of amount from cart be subtracted from the value of stock.购买后,我想从库存价值中减去购物车金额的价值。

item = (Item.update(stock=20)
            .where(Item.id.in_
                   (Cart.select(Cart.item_id)
                    .where((Cart.user == current_id) & (Cart.payment_status == False)))))
    item.execute()

Ive gotten stuck here.我被困在这里了。 I can only hard code the stock value.我只能硬编码股票价值。 Im not sure how to subtract Cart.amount from Item.stock我不确定如何从 Item.stock 中减去 Cart.amount

ok finally found a solution ok 终于找到解决办法了

cart_amount = Cart.select(Cart.amount).where(Cart.item_id == Item.id)

        item = (Item.update(stock=Item.stock - cart_amount)
                .where(Item.id.in_
                       (Cart.select(Cart.item_id)
                        .where((Cart.user == current_id) & (Cart.payment_status == False)))))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据使用 Pyspark 的条件从另一个表更新 Spark DataFrame 表的列值 - How to update Spark DataFrame Column Values of a table from another table based on a condition using Pyspark 在python中如何从更新的peewee模式更新psql表列 - In python how to update psql Table column from updated peewee schema 我如何将其转换为peewee - How do I convert this into peewee 如何根据条件添加列并根据另一列值的语法为其分配值? - How do I add a column based on a condition and assign values to it based on the syntax of values from another column? 如何根据条件从另一个 Dataframe 更新 Dataframe 值 - How to update a Dataframe values from an another Dataframe based on condition 如何根据条件从 Python 更新 Postgres 表中的列值 - How to update column value in Postgres table from Python based on condition SQLalchemy 如何根据另一个表的日期从另一个表查询? - SQLalchemy how do I query from another table based on the dates from another table? 如何根据另一个表上的条件用 SQLAlchemy 更新表。 .Join() 不适用于.Update() - How to update a table with SQLAlchemy based on a condition on another table. .Join() doesn't work with .Update() 如何根据 python 中另一列的条件查找两个日期之间特定列的最大值 - How do I Find max value of a particular column between 2 dates based on a condition from another column in python 如何使用peewee重新选择记录 - How to reselect record with peewee
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM