I am trying to add 'items', 'price', 'stock' and eventually 'dates' into my inventory.db using the table called Product. 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? 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 .
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. 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.