简体   繁体   English

为什么我会收到 peewee.OperationalError

[英]Why am i getting peewee.OperationalError

I am trying to add 'items', 'price', 'stock' and eventually 'dates' into my inventory.db using the table called Product.我正在尝试使用名为 Product 的表将“items”、“price”、“stock”和最终“dates”添加到我的inventory.db中。 How do I get that data in there without bombing the code.我如何在不轰炸代码的情况下获取这些数据。 Since I'm new to this type of stuff please go into detail on what I'm doing wrong.由于我是这类东西的新手,请详细说明我做错了什么。

Also how can I make an ID for each individual product name?另外,如何为每个单独的产品名称制作 ID? I've been looking and I just don't see what I'm looking for.我一直在寻找,但我没有看到我在寻找什么。

Here's what I'm working with: https://github.com/OXDavidXO/Python-Project-4/blob/main/app.py .这是我正在使用的内容: https : //github.com/OXDavidXO/Python-Project-4/blob/main/app.py

Here's the part that is messing up:这是搞砸的部分:

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'])

I'm guessing you are using Postgresql, although you didn't mention what db.我猜你正在使用 Postgresql,虽然你没有提到什么 db. When you get into a bad transactional state (eg by catching the integrity error) then you need to rollback to a good state to continue.当您进入不良事务状态(例如,通过捕获完整性错误)时,您需要回滚到良好状态才能继续。 The proper way is to:正确的做法是:

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'])

Otherwise, please post the actual traceback and error message.否则,请发布实际的回溯和错误消息。 It is hard to debug without this information.没有这些信息就很难调试。

暂无
暂无

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

相关问题 peewee.OperationalError:没有这样的 function:json - peewee.OperationalError: no such function: json Flask:peewee.OperationalError:没有这样的表: - Flask: peewee.OperationalError: no such table: peewee 多进程来自单独的数据库连接“peewee.OperationalError: 磁盘 I/O 错误” - peewee multiprocess from separate db connection 'peewee.OperationalError: disk I/O error' peewee.OperationalError:在“ AS”附近:语法错误 - peewee.OperationalError: near “AS”: syntax error peewee.OperationalError:表要点没有名为name的列 - peewee.OperationalError: table gist has no column named name peewee.OperationalError:由于未完成的语句或未完成的备份而无法关闭 - peewee.OperationalError: unable to close due to unfinalized statements or unfinished backups 在python unittest中,保存Peewee对象的实例会引发peewee.IntegrityError:和peewee.OperationalError: - In python unittest, saving an instance of Peewee object raises peewee.IntegrityError: and peewee.OperationalError: 为什么我突然收到“OperationalError:没有这样的表”? - Why am I suddenly getting "OperationalError: no such table"? peewee.OperationalError:只有150行* 8列的upsert上有太多的SQL变量 - peewee.OperationalError: too many SQL variables on upsert of only 150 rows * 8 columns 为什么在使用pegreee ORM和posgresql的一个失败命令之后,为什么所有后续命令都出现peewee.InternalError? - Why am I getting peewee.InternalError for all subsequent commands after one failed command, using peewee ORM with posgresql?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM