Opened 7 years ago
Last modified 7 years ago
#30292 closed Bug
The unique_together meta option does not get migrated — at Initial Version
| 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
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_togehter = (("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.