U
U
username_already_exists2021-05-27 10:01:40
Python
username_already_exists, 2021-05-27 10:01:40

How to add unique records to the database, but only for the date that is not yet in the database?

I'm starting to practice with databases, and decided to use the peewee ORM. For training, I decided to create a table with expense records and fields: purchase date, product name (unique field), product price.
The model looks like this:

class BaseModel(Model):
    class Meta:
        database = db

class Purchases(BaseModel):
    purchase_day = CharField()
    product = CharField(unique=True)
    price = FloatField()


The problem is that when adding another date of purchase and the product name matches, they get an error because the product name field is unique.
What should be done in such a case? Remove uniqueness from the "product" field, and check with a query when adding a record, or is there another way?

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question