#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:
> 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.
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",),)
}}}
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.
--
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 <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/067.a708e03f79c969f3e52034bc070ef2c8%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.