makemigrations complains about a ForeignKey not being Unique

377 views
Skip to first unread message

Samuel Jean

unread,
Jan 22, 2015, 3:17:08 AM1/22/15
to django...@googlegroups.com
Hi there,

Does anybody know of a way to trick Django 1.7 (or the proper way, if
any) to modify the property of an inherited field so that it has unique
property on the children only.

Consider this abstract model :

class UniqueObjectModel(models.Model):
uuid = models.CharField(max_length=36, default=uuid4)
pool = models.ForeignKey('Pool', related_name='+', to_field='uuid')
...

class Meta:
abstract = True
unique_together = (('uuid', 'pool'),)

I would like the Pool model to inherit from UniqueObjectModel *but* have
its uuid field unique.

class Pool(UniqueObjectModel):
def __init__(self, *args, **kwargs):
super(Pool, self).__init__(*args, **kwargs)
self._meta.get_field('pool').to = 'self'
self._meta.get_field('uuid')._unique = True

However, Django still complains about Pool.uuid field not being unique.

$ python manage.py makemigrations myapp

CommandError: System check identified some issues:

ERRORS:
myapp.Controller.pool: (fields.E311) 'Pool.uuid' must set unique=True
because it is referenced by a foreign key.

Any ideas?

Thanks!

Collin Anderson

unread,
Jan 23, 2015, 2:15:38 AM1/23/15
to django...@googlegroups.com, js...@herness.org
Hi,

I think you might want to use a common abstract base class. You want a completely separate database table for the two models, right?

Collin
Reply all
Reply to author
Forward
0 new messages