[Django] #23564: Django migration fails on unqiue_together with model subclass references

5 views
Skip to first unread message

Django

unread,
Sep 28, 2014, 9:09:16 AM9/28/14
to django-...@googlegroups.com
#23564: Django migration fails on unqiue_together with model subclass references
----------------------------+--------------------
Reporter: dbinetti | Owner: nobody
Type: Bug | Status: new
Component: Migrations | Version: 1.7
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------+--------------------
I've searched around and can't find a bug for this behavior, so here
goes...

With this model inheritance:

{{{
class Account(models.Model):
name = models.CharField (...)

class Source(models.Model):
name = models.CharField(...)
account = models.ForeignKey('Account', related_name='sources')

class SubSource(Source):
uid = models.CharField(...)

class Meta:
unique_together = (
("account", "uid"),
)
}}}

My intended behavior here is to ensure I can only have a single
combination of Accounts and (external) SubSources. I may have multiple
instances of the same external sub-source on the table if used by
different accounts, which is why i'm not simply using `unique` on the
`uid` field.

I can run `django-admin.py check`, which passes, and `django-admin.py
makemigrations`, which passes. But when i try to run `django-admin.py
migrate` it fails with:

{{{
django.db.utils.ProgrammingError: column "account_id" named in key does
not exist
}}}

the generated migration script looks like this:

{{{
# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations


class Migration(migrations.Migration):

dependencies = [
<snip>
]

operations = [
migrations.AlterUniqueTogether(
name='subsource',
unique_together=set([('account', 'uid')]),
),
]
}}}


I'm sorry that I'm not skilled enough to produce an actual test case.
Thanks.

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

Django

unread,
Sep 28, 2014, 10:45:14 AM9/28/14
to django-...@googlegroups.com
#23564: Django migration fails on unqiue_together with model subclass references
-------------------------------------+-------------------------------------
Reporter: dbinetti | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 1.7
(models, ORM) | Resolution: duplicate
Severity: Normal | Triage Stage:
Keywords: checks | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by charettes):

* status: new => closed
* needs_better_patch: => 0
* component: Migrations => Database layer (models, ORM)
* needs_tests: => 0
* keywords: => checks
* needs_docs: => 0
* resolution: => duplicate


Comment:

The real issue here is that you shouldn't be allowed to include non-local
fields in `unique_together`. An additional check was added in #22356 which
has been merged into master.

I'm wondering why I didn't backport it to 1.7 right away. Since this can
lead to invalid migrations being created we might want to include it in
1.7.1?

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

Reply all
Reply to author
Forward
0 new messages