Opened 6 years ago

Last modified 6 years ago

#30292 closed Bug

The unique_together meta option does not get migrated — at Version 2

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 Calin Bule)

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 (2)

comment:1 by Mariusz Felisiak, 6 years ago

Resolution: invalid
Status: newclosed
Version: 2.1master

It looks that you made a typo in the Meta option, it should be unique_together instead of unique_togehter.

comment:2 by Calin Bule, 6 years ago

Cc: Calin Bule added
Description: modified (diff)
Resolution: invalid
Status: closednew

I corrected the typo and it still does not work. It doesn't ignore everything in the Meta subclass, just the unique_together option.

Note: See TracTickets for help on using tickets.
Back to Top