Opened 10 years ago
Closed 10 years ago
#23564 closed Bug (duplicate)
Django migration fails on unqiue_together with model subclass references
Reported by: | David Binetti | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.7 |
Severity: | Normal | Keywords: | checks |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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.
Change History (1)
comment:1 by , 10 years ago
Component: | Migrations → Database layer (models, ORM) |
---|---|
Keywords: | checks added |
Resolution: | → duplicate |
Status: | new → closed |
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?