1
0
mirror of https://github.com/django/django.git synced 2024-12-22 17:16:24 +00:00

Refs #31310 -- Added test for check for using intermediate model with ambiguous foreign key from model.

This commit is contained in:
Matheus Cunha Motta 2020-02-27 12:57:53 -03:00 committed by Mariusz Felisiak
parent a4881f5e5d
commit 308fab9241

View File

@ -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):