Skip to content

Commit

Permalink
Fixed #11702: Catch to_field specifying a non-unique target in valida…
Browse files Browse the repository at this point in the history
…tion. Thanks marcosmoyano.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@12756 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information
kmtracey committed Mar 10, 2010
1 parent d312fa2 commit 079457f
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 4 additions & 0 deletions django/core/management/validation.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,10 @@ def get_validation_errors(outfile, app=None):
if isinstance(f.rel.to, (str, unicode)):
continue

# Make sure the related field specified by a ForeignKey is unique
if not f.rel.to._meta.get_field(f.rel.field_name).unique:
e.add(opts, "Field '%s' under model '%s' must have a unique=True constraint." % (f.rel.field_name, f.rel.to.__name__))

rel_opts = f.rel.to._meta
rel_name = RelatedObject(f.rel.to, cls, f).get_accessor_name()
rel_query_name = f.related_query_name()
Expand Down
24 changes: 23 additions & 1 deletion tests/modeltests/invalid_models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,27 @@ class AbstractRelationModel(models.Model):

class UniqueM2M(models.Model):
""" Model to test for unique ManyToManyFields, which are invalid. """
unique_people = models.ManyToManyField( Person, unique=True )
unique_people = models.ManyToManyField(Person, unique=True)

class NonUniqueFKTarget1(models.Model):
""" Model to test for non-unique FK target in yet-to-be-defined model: expect an error """
tgt = models.ForeignKey('FKTarget', to_field='bad')

class UniqueFKTarget1(models.Model):
""" Model to test for unique FK target in yet-to-be-defined model: expect no error """
tgt = models.ForeignKey('FKTarget', to_field='good')

class FKTarget(models.Model):
bad = models.IntegerField()
good = models.IntegerField(unique=True)

class NonUniqueFKTarget2(models.Model):
""" Model to test for non-unique FK target in previously seen model: expect an error """
tgt = models.ForeignKey(FKTarget, to_field='bad')

class UniqueFKTarget2(models.Model):
""" Model to test for unique FK target in previously seen model: expect no error """
tgt = models.ForeignKey(FKTarget, to_field='good')


model_errors = """invalid_models.fielderrors: "charfield": CharFields require a "max_length" attribute.
Expand Down Expand Up @@ -279,4 +299,6 @@ class UniqueM2M(models.Model):
invalid_models.abstractrelationmodel: 'fk1' has a relation with model AbstractModel, which has either not been installed or is abstract.
invalid_models.abstractrelationmodel: 'fk2' has an m2m relation with model AbstractModel, which has either not been installed or is abstract.
invalid_models.uniquem2m: ManyToManyFields cannot be unique. Remove the unique argument on 'unique_people'.
invalid_models.nonuniquefktarget1: Field 'bad' under model 'FKTarget' must have a unique=True constraint.
invalid_models.nonuniquefktarget2: Field 'bad' under model 'FKTarget' must have a unique=True constraint.
"""

0 comments on commit 079457f

Please sign in to comment.