[英]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.