[Django] #30292: The unique_together meta option does not get migrated

24 views
Skip to first unread message

Django

unread,
Mar 26, 2019, 7:49:15 AM3/26/19
to django-...@googlegroups.com
#30292: The unique_together meta option does not get migrated
-------------------------------------+-------------------------------------
Reporter: Calin | Owner: nobody
Bule |
Type: Bug | Status: new
Component: | Version: 2.1
Migrations | Keywords: migrations, unique-
Severity: Normal | together, constraint
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/30292>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Mar 26, 2019, 7:57:05 AM3/26/19
to django-...@googlegroups.com
#30292: The unique_together meta option does not get migrated
-------------------------------------+-------------------------------------
Reporter: Calin Bule | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: master
Severity: Normal | Resolution: invalid
Keywords: migrations, unique- | Triage Stage:
together, constraint | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => closed
* version: 2.1 => master
* resolution: => invalid


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/30292#comment:1>

Django

unread,
Mar 26, 2019, 9:52:42 AM3/26/19
to django-...@googlegroups.com
#30292: The unique_together meta option does not get migrated
-------------------------------------+-------------------------------------
Reporter: Calin Bule | Owner: nobody
Type: Bug | Status: new

Component: Migrations | Version: master
Severity: Normal | Resolution:
Keywords: migrations, unique- | Triage Stage:
together, constraint | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Calin Bule):

* cc: Calin Bule (added)
* status: closed => new
* resolution: invalid =>


Old description:

New 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_together = (("person_1", "person_2",),)
}}}

--

Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/30292#comment:2>

Django

unread,
Mar 26, 2019, 10:00:21 AM3/26/19
to django-...@googlegroups.com
#30292: The unique_together meta option does not get migrated
-------------------------------------+-------------------------------------
Reporter: Calin Bule | Owner: nobody
Type: Bug | Status: closed
Component: Migrations | Version: master
Severity: Normal | Resolution: invalid

Keywords: migrations, unique- | Triage Stage:
together, constraint | Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* status: new => closed

* resolution: => invalid


Comment:

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
[https://code.djangoproject.com/wiki/TicketClosingReasons/UseSupportChannels
support channels].

--
Ticket URL: <https://code.djangoproject.com/ticket/30292#comment:3>

Reply all
Reply to author
Forward
0 new messages