Opened 7 years ago
Closed 7 years ago
#30292 closed Bug (invalid)
The unique_together meta option does not get migrated
| Reported by: | Calin Bule | Owned by: | nobody |
|---|---|---|---|
| Component: | Migrations | Version: | dev |
| Severity: | Normal | Keywords: | migrations, unique-together, constraint |
| Cc: | Calin Bule | Triage Stage: | Unreviewed |
| Has patch: | no | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description (last modified by )
I am developing an application in Django 2.1.7 + PostgreSQL, on a Windows 10 machine. I'm trying to create a unique constraint of a composed key using the unique_together option in the Meta class of a model, but it won't migrate, nor does Django return an error message.
I went directly into the database engine and created the constraint manually with and it works well both in the db and Django admin.
Django model code:
class PersonRelationship(db_table):
person_1 = models.ForeignKey(AccountPerson, on_delete=models.CASCADE, related_name="person_1", null=False)
person_2 = models.ForeignKey(AccountPerson, on_delete=models.CASCADE, related_name="person_2", null=False)
relationship_type = models.ForeignKey(PersonRelationshipType, on_delete=models.CASCADE, null=False)
def __str__(self):
return str(self.person_1) + " - " + str(self.person_2) + " " + str(self.relationship_type)
class Meta:
options.unique_together = (("person_1", "person_2",),)
SQL code used to manually create the constraint:
ALTER TABLE public.accounts_personrelationship ADD CONSTRAINT accounts_personrelationship_un UNIQUE (person_1_id,person_2_id,relationship_type_id);
After generating the migration file, I manually inserted the code for the generation of the constraint:
migrations.AlterUniqueTogether(
name='personaccountsrelationship',
unique_together={('account_1', 'account_2')},
),
I then ran the migrate command and checked the db and the constraint was created. I then made other modifications and ran makemigrations again and, among other stuff I found this:
class Migration(migrations.Migration):
dependencies = [
('accounts', '0001_initial'),
]
operations = [
migrations.AlterUniqueTogether(
name='personrelationship',
unique_together=set(),
),
]
It deleted the constraint I previously created manually.
So, not only the constraints do not get created automatically, but they get deleted when I run further migrations.
I tried running the migration on a MacOS X Mojave and it works well. On Windows though, I can't seem to get it to work.
Change History (3)
comment:1 by , 7 years ago
| Resolution: | → invalid |
|---|---|
| Status: | new → closed |
| Version: | 2.1 → master |
comment:2 by , 7 years ago
| Cc: | added |
|---|---|
| Description: | modified (diff) |
| Resolution: | invalid |
| Status: | closed → new |
I corrected the typo and it still does not work. It doesn't ignore everything in the Meta subclass, just the unique_together option.
comment:3 by , 7 years ago
| Resolution: | → invalid |
|---|---|
| Status: | new → closed |
You should use unique_together not options.unique_together. Please do not reopen this ticket, because it is not a bug in Django it is a support issue, use one of support channels.
It looks that you made a typo in the Meta option, it should be
unique_togetherinstead ofunique_togehter.