From 308fab924195bdea2fc481e1466c3c3894c03400 Mon Sep 17 00:00:00 2001 From: Matheus Cunha Motta Date: Thu, 27 Feb 2020 12:57:53 -0300 Subject: [PATCH] Refs #31310 -- Added test for check for using intermediate model with ambiguous foreign key from model. --- .../test_relative_fields.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/tests/invalid_models_tests/test_relative_fields.py b/tests/invalid_models_tests/test_relative_fields.py index 24f27168c2..a23e8bf044 100644 --- a/tests/invalid_models_tests/test_relative_fields.py +++ b/tests/invalid_models_tests/test_relative_fields.py @@ -128,6 +128,36 @@ class RelativeFieldTests(SimpleTestCase): ), ]) + def test_ambiguous_relationship_model_from(self): + class Person(models.Model): + pass + + class Group(models.Model): + field = models.ManyToManyField('Person', through='AmbiguousRelationship') + + class AmbiguousRelationship(models.Model): + person = models.ForeignKey(Person, models.CASCADE) + first_group = models.ForeignKey(Group, models.CASCADE, related_name='first') + second_group = models.ForeignKey(Group, models.CASCADE, related_name='second') + + field = Group._meta.get_field('field') + self.assertEqual(field.check(from_model=Group), [ + Error( + "The model is used as an intermediate model by " + "'invalid_models_tests.Group.field', but it has more than one " + "foreign key from 'Group', which is ambiguous. You must " + "specify which foreign key Django should use via the " + "through_fields keyword argument.", + hint=( + 'If you want to create a recursive relationship, use ' + 'ForeignKey("self", symmetrical=False, ' + 'through="AmbiguousRelationship").' + ), + obj=field, + id='fields.E334', + ), + ]) + def test_ambiguous_relationship_model(self): class Person(models.Model):