question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unique together constraints including the locale cause IntregrityError

See original GitHub issue

Issue 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

  1. Get a current version of the Wagtail bakery demo project.

  2. 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'),
            ]
    
  3. Run ./manage.py makemigrations && ./manage.py migrate && ./manage.py runserver.

  4. Go to http://localhost:8000/admin/snippets/breads/breadcrumbs/

  5. Click the “add bread cumbs” button

  6. Enter “test” in the title field and save the snippet.

  7. Click the “add bread cumbs” button again

  8. Enter “test” in the title field and save the snippet agin.

  9. Observe the IntegrityError: image

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: image

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:open
  • Created a year ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
allcapscommented, Aug 8, 2022

The ‘locale’ field has ‘editable=False’ iirc.

Field.editable¶ If False, the field will not be displayed in the admin or any other ModelForm. They are also skipped during model validation. Default is True. https://docs.djangoproject.com/en/4.1/ref/models/fields/#editable

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.

1reaction
tbrlpldcommented, Aug 7, 2022

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why doesn't Django enforce my unique_together constraint as ...
If I don't explicitly check the unique_together constraint in the clean_title() function, django throws an exception: IntegrityError at /journal/journal/4.
Read more >
How to Deal With UNIQUE Fields on a Multi-lingual Site
How to deal with fields with UNIQUE constraint integrity errors when translating pages in Django and Wagtail using TranslatableMixin.
Read more >
Resolving ORA-00001 ERROR: Unique Constraint Violated ...
There are three potential issues: The Masked Value is not Unique and hence duplicated. The Masked Value is unique but it is already...
Read more >
Solved: VMware Workstation installer fails on Fedora 31
The root cause lies in "sqlite3.IntegrityError: UNIQUE constraint failed: files.path", which tries to insert an item to the sqlite database during installing ...
Read more >
Does it required a slugfield's must be unique in django?
because my problem is when i update my existed object it throws an error IntegrityError: UNIQUE constraint failed: polls_question.id. score:0.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found