簡體   English   中英

為什么我會收到 peewee.OperationalError

[英]Why am i getting peewee.OperationalError

我正在嘗試使用名為 Product 的表將“items”、“price”、“stock”和最終“dates”添加到我的inventory.db中。 我如何在不轟炸代碼的情況下獲取這些數據。 由於我是這類東西的新手,請詳細說明我做錯了什么。

另外,如何為每個單獨的產品名稱制作 ID? 我一直在尋找,但我沒有看到我在尋找什么。

這是我正在使用的內容: https : //github.com/OXDavidXO/Python-Project-4/blob/main/app.py

這是搞砸的部分:

inventory = {
    'items': food_names,
    'price': food_price,
    'stock': food_stock,
    'dates': dates_added
}


def add_products():

    try:
        food_item = Product.create(product_names = inventory['items'])
        food_item.save()
    except IntegrityError:
        food_product = Product.get(product_names = inventory['items'])

我猜你正在使用 Postgresql,雖然你沒有提到什么 db. 當您進入不良事務狀態(例如,通過捕獲完整性錯誤)時,您需要回滾到良好狀態才能繼續。 正確的做法是:

try:
    with db.atomic() as tx:
        food_item = Product.create(product_names = inventory['items'])
        # calling save() again is redundant, removed.
except IntegrityError as exc:
    food_item = Product.get(product_names = inventory['items'])

否則,請發布實際的回溯和錯誤消息。 沒有這些信息就很難調試。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM