wagtail / wagtail-localize

Translation plugin for Wagtail CMS
https://wagtail-localize.org/
Other
222 stars 84 forks source link

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

Open marcosguedes opened 10 months ago

marcosguedes commented 10 months ago

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 commented 10 months ago

@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 commented 10 months ago

@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 commented 10 months ago

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