4

One of my views has the task of inserting several values into my database. I was under the impression that if I set up my model with unique vendor_names:

class Page(models.Model):
  vendor_name = models.CharField(max_length=128, unique=True)
  website = models.CharField(max_length=128)
  food_type = models.CharField(max_length=128)
  img_url = models.CharField(max_length=128)

that if I did:

  for vendor in vendors:
    c = Page(vendor_name=vendor["name"], 
             website=vendor["link"], 
             food_type=vendor["type"],
             img_url=vendor["imageurl"])
    c.save()

The appearance of duplicates would be skipped over and I would only have one copy in the database. At least thats what I understood from here. Instead of this do I have to add an if statement thats checks my database for each entry and sees if it currently is in there, if it isn't insert otherwise skip. Or am I missing something here? Whats the purpose of the unique constraint? Is it just to throw an error when there is a duplicate? Could I maybe exploit this instead?

The error I get is

Exception Value: duplicate key value violates unique constraint...
5
  • According to the docs: If True, this field must be unique throughout the table. This is enforced at the database level and by model validation. If you try to save a model with a duplicate value in a unique field, a django.db.IntegrityError will be raised by the model’s save() method. Commented Jul 19, 2015 at 4:34
  • Why not use a foreign key instead, since there seems to be a Vendor model Commented Jul 19, 2015 at 4:34
  • So yes, it's supposed to not repeat. Do you have unapplied migrations? Commented Jul 19, 2015 at 4:34
  • @AndréFratelli My recent migrations have been applied. I get an error though. See above. Commented Jul 19, 2015 at 4:40
  • @Pynchia there is no Vendor model, but can you explain how using a foreign key would help me here? Vendor is coming from an array of objects. Commented Jul 19, 2015 at 4:40

2 Answers 2

2

In Django, unique enforces a database level validation of entries so if you add that property to your model's field after the table was already created, the unique condition will not be added to your table even if you do syncdb later at some point.

If you don't want to create rows with same vendor_name, you should use Page.objects.get_or_crate to let Django create Page object with that vendor name only if not exists:

for vendor in vendors:
    page, created = Page.objects.get_or_create(
        vendor_name=vendor['name'], 
        defaults={'website': vendor['link'], 
                  'food_type': vendor['type'], 
                  'img_url': vendor['imageurl'])

    if created:
        print('Page created: ', page)
Sign up to request clarification or add additional context in comments.

Comments

0

You have unapplied migrations. The fact that you get Exception Value: duplicate key value violates unique constraint means that you need to clear duplications from your database before applying the migrations. You cannot just add constraints that are already being violated.

In case that's an option, delete the entire table from the database and try applying your migrations again. If that's not an option you'll need to delete the duplications and apply the migrations afterwards.

Without applying the migrations unique will not work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.