dimanche 30 juillet 2017

Django unique combination is duplicate

django.db.utils.IntegrityError: (1062, "Duplicate entry 'Restauarant' for key 'restaurants_restaurant_name_address_id_e074fa2e_uniq'")

Above is the error. As shown in the model for a restaurant I have defined that the address and name should be unique together. Though when I create a restaurant, if the combination of name and address are unique but either of them are the same as another restaurant object then this fails. E.G. "Restaurant" at "10 Something Street" would fail if there were an existing object like "Restaurant" at "35 Something Else Street." Any suggestions would be great, thanks.

class Restaurant(models.Model):
    user = models.ForeignKey(User)
    name = models.CharField(max_length=128)
    street_number = models.CharField(max_length=10)
    route = models.CharField(max_length=128)
    locality = models.CharField(max_length=116)  # 2* 58 (longest place name)
    administrative_area_level_1 = models.CharField(max_length=116, default=None)  
    postal_code = models.CharField(max_length=12)
    country = CountryField()
    slug = models.SlugField(max_length=128)
    profile_image = models.ImageField(upload_to='profile_images', blank=True)
    menu = models.ManyToManyField(Menu)
    favourites = models.IntegerField(default=0, editable=False)
    is_active = models.BooleanField(default=True)

    class Meta:
        # ensures unique combination, prevents duplicate 'restaurants'
        unique_together = ((
            'name',
            'street_number',
            'route',
            'locality',
            'administrative_area_level_1',
            'postal_code',
            'country'
        ))



Aucun commentaire:

Enregistrer un commentaire