Unique together constraints including the locale cause IntregrityError
See original GitHub issueIssue Summary
When using the locale in unique_together
or a UniqueConstraint
on a snippet then submitting a violating form leads to a IntegrityError
.
Steps to Reproduce
-
Get a current version of the Wagtail bakery demo project.
-
Edit
bakerydemo/breads/models.py
as follows.@register_snippet class BreadCrumbs(TranslatableMixin, models.Model): title = models.CharField(max_length=255) panels = [ FieldPanel('title'), ] def __str__(self): return self.title class Meta(TranslatableMixin.Meta): unique_together = TranslatableMixin.Meta.unique_together + [ ('title', 'locale'), ]
-
Run
./manage.py makemigrations && ./manage.py migrate && ./manage.py runserver
. -
Go to http://localhost:8000/admin/snippets/breads/breadcrumbs/
-
Click the “add bread cumbs” button
-
Enter “test” in the title field and save the snippet.
-
Click the “add bread cumbs” button again
-
Enter “test” in the title field and save the snippet agin.
-
Observe the
IntegrityError
:
Any other relevant information. For example, why do you consider this a bug and what did you expect to happen instead?
The expected behavior would be to see an error message like the following:
I have tried other variants of unique_together
constrains, e.g. two char fields or a char field and another foreign key field (other than the locale) and they all work and lead to nice error messages like the one shown above.
Here is a different example snippet with a working unique_together
constraint to a foreign key.
@register_snippet
class CrumbType(models.Model):
title = models.CharField(max_length=255)
ingredient = models.ForeignKey(to=BreadIngredient, related_name="+", on_delete=models.PROTECT)
panels = [
FieldPanel('title'),
FieldPanel('ingredient'),
]
def __str__(self):
return self.title
class Meta:
unique_together = [
('title', 'ingredient'),
]
- I have confirmed that this issue can be reproduced as described on a fresh Wagtail project: (yes /
no)
Technical details
- Python version: Run
python --version
. Python 3.9.13 - Django version: Look in your requirements.txt, or run
pip show django | grep Version
. Version: 4.0.5 - Wagtail version: Look at the bottom of the Settings menu in the Wagtail admin, or run
pip show wagtail | grep Version:
. Version: 3.0.1 - Browser version: You can use https://www.whatsmybrowser.org/ to find this out. – Not a browser issue -> Any
Issue Analytics
- State:
- Created a year ago
- Comments:5 (3 by maintainers)
Top GitHub Comments
The ‘locale’ field has ‘editable=False’ iirc.
I do get why direct editing of this field is prohibited, but I wouldn’t be against a less restrictive approach and make this field editable by removing the attr.
Same situation applies for the uid field.
Hi @Sanyam-Garg. Thanks for replicating this!
Unfortunately I am unfamiliar with the view / validation logic myself, so I won’t be of much help there.