Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IntegrityError: could not create unique index when adding translation mixins to created objects #737

Open
marcosguedes opened this issue Nov 6, 2023 · 3 comments

Comments

@marcosguedes
Copy link

Hi, I'm running Wagtail 5.0 and wagtail-localize 1.6 and I'm getting an issue when adding translation to current models

I followed the tutorial up to the part where we translate content. I create a new migration where Wagtail suggests me to create a unique_together constraint. I do so but it still remains.

$ ./manage.py makemigrations
ERRORS:
portfolio.ProjectCategoryName: (wagtailcore.E003) portfolio.ProjectCategoryName is missing a unique_together constraint for the translation key and locale field
        HINT: Add ('translation_key', 'locale') to ProjectCategoryName.Meta.unique_together
+...
+from wagtail.models import TranslatableMixin
+...


-class ProjectCategoryName(Orderable):
+class ProjectCategoryName(Orderable, TranslatableMixin):
    name = models.CharField(max_length=255)

    panels = [
        FieldPanel("name"),
    ]

    class Meta:
        verbose_name = _("Project Category")
        verbose_name_plural = _("Project Categories")
        ordering = ("name",)
+        unique_together = ("translation_key", "locale")

    def __str__(self):
        return self.name

It then asks me for a default. I use the first locale ID

$ ./manage.py makemigrations portfolio
It is impossible to add a non-nullable field 'locale' to projectcategoryname without specifying a default. This is because the database needs something to populate existing rows.
Please select a fix:
 1) Provide a one-off default now (will be set on all existing rows with a null value for this column)
 2) Quit and manually define a default value in models.py.
Select an option: 1
>>> 1

Then it breaks when I'm migrating

django.db.utils.IntegrityError: could not create unique index "portfolio_projectcategor_translation_key_locale_i_77fbe046_uniq"
DETAIL:  Key (translation_key, locale_id)=(cd19e8c3-81be-4da7-8a45-c78cb26c69e1, 1) is duplicated.

Is there something I can do to fix this? Or adding translations requires me to delete everything and start over?

Apologies if this is a newbie question unrelated to the package or it's addressed somewhere in the documentation but it's my first time with Wagtail and my eyes are burning.

Cheers

@zerolab
Copy link
Collaborator

zerolab commented Nov 6, 2023

@marcosguedes please see https://docs.wagtail.org/en/stable/advanced_topics/i18n.html#making-snippets-with-existing-data-translatable for instructions on how to make models with content translatable

If you have suggestions on how to improve the documentation, please do share and even better, submit a PR

@marcosguedes
Copy link
Author

@zerolab many thanks. I believe the issue here was due to being fully guided by wagtail-localize.org. I can submit a PR with a warning in this section pointing to the docs if you believe that's the right approach

@zerolab
Copy link
Collaborator

zerolab commented Nov 6, 2023

@marcosguedes a warning would most certainly be useful there, thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants