Skip to content

Commit 0beac34

Browse files
committed
fix: Fix patching unique_together for migrations
Refs #785
1 parent 85a2ac1 commit 0beac34

File tree

3 files changed

+11
-1
lines changed

3 files changed

+11
-1
lines changed

modeltranslation/tests/migrations/0001_initial.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Generated by Django 5.2.4 on 2025-09-14 04:17
1+
# Generated by Django 5.2.4 on 2025-09-14 05:04
22

33
import django.contrib.auth.models
44
import django.core.validators
@@ -943,6 +943,11 @@ class Migration(migrations.Migration):
943943
name="unique_fields-sub_title_en",
944944
),
945945
],
946+
"unique_together": {
947+
("title", "sub_title"),
948+
("title", "sub_title_de"),
949+
("title", "sub_title_en"),
950+
},
946951
},
947952
),
948953
migrations.CreateModel(

modeltranslation/tests/models.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class ModelWithConstraint(models.Model):
2727
sub_title = models.CharField(max_length=255)
2828

2929
class Meta:
30+
unique_together = (("title", "sub_title"),)
3031
constraints = [
3132
models.UniqueConstraint(
3233
fields=["title", "sub_title"],

modeltranslation/translator.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,10 @@ def add_constraints():
357357

358358
model._meta.unique_together += tuple(add_unique_together()) # type: ignore[operator]
359359
model._meta.constraints += tuple(add_constraints())
360+
# `unique_together` needs `original_attrs` to be set, for this changes to appear in migrations.
361+
for attr_name in ("unique_together",):
362+
if value := getattr(model._meta, attr_name):
363+
model._meta.original_attrs[attr_name] = value
360364

361365

362366
def delete_mt_init(sender: type[Model], instance: Model, **kwargs: Any) -> None:

0 commit comments

Comments
 (0)