[Django] #31310: Wrong hint about recursive relationship

9 views
Skip to first unread message

Django

unread,
Feb 26, 2020, 1:07:19 PM2/26/20
to django-...@googlegroups.com
#31310: Wrong hint about recursive relationship
-------------------------------------+-------------------------------------
Reporter: Matheus | Owner: nobody
Cunha Motta |
Type: | Status: new
Cleanup/optimization |
Component: Database | Version: 3.0
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 1
UI/UX: 0 |
-------------------------------------+-------------------------------------
When there's more than 2 ForeignKeys pointing to the same model in an
intermediary model of a m2m field and no through_fields have been set,
Django will show an error with the following hint:
{{{#!python
hint=(
'If you want to create a recursive relationship, '
'use ForeignKey("%s", symmetrical=False, through="%s").'
}}}
But to create a recursive relationship the ManyToManyField should be used
instead of ForeignKey. Also symmetrical=False should only be used with m2m
fields, according to model field reference (link:
https://docs.djangoproject.com/en/3.0/ref/models/fields/#django.db.models.ManyToManyField.symmetrical).
And more than that, setting symmetrical=False is not required for
recursive relationships since Django >= 3.0.

This was probably a small mistake where the developer thought
ManyToManyField but typed ForeignKey instead. And the symmetrical=False is
an outdated requirement to recursive relationships, not applicable since
3.0. I'll provide a PR with a proposed correction shortly after.

--
Ticket URL: <https://code.djangoproject.com/ticket/31310>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Feb 26, 2020, 1:14:13 PM2/26/20
to django-...@googlegroups.com
#31310: Wrong hint about recursive relationship
-------------------------------------+-------------------------------------
Reporter: Matheus Cunha Motta | Owner: nobody
Type: | Status: new
Cleanup/optimization |
Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Matheus Cunha Motta):

Here's a PR: https://github.com/django/django/pull/12497

--
Ticket URL: <https://code.djangoproject.com/ticket/31310#comment:1>

Django

unread,
Feb 26, 2020, 7:33:04 PM2/26/20
to django-...@googlegroups.com
#31310: Wrong hint about recursive relationship
-------------------------------------+-------------------------------------
Reporter: Matheus Cunha Motta | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Description changed by Matheus Cunha Motta:

Old description:

> When there's more than 2 ForeignKeys pointing to the same model in an
> intermediary model of a m2m field and no through_fields have been set,
> Django will show an error with the following hint:
> {{{#!python
> hint=(
> 'If you want to create a recursive relationship, '
> 'use ForeignKey("%s", symmetrical=False, through="%s").'
> }}}
> But to create a recursive relationship the ManyToManyField should be used
> instead of ForeignKey. Also symmetrical=False should only be used with
> m2m fields, according to model field reference (link:
> https://docs.djangoproject.com/en/3.0/ref/models/fields/#django.db.models.ManyToManyField.symmetrical).
> And more than that, setting symmetrical=False is not required for
> recursive relationships since Django >= 3.0.
>
> This was probably a small mistake where the developer thought
> ManyToManyField but typed ForeignKey instead. And the symmetrical=False
> is an outdated requirement to recursive relationships, not applicable
> since 3.0. I'll provide a PR with a proposed correction shortly after.

New description:

When there's more than 2 ForeignKeys in an intermediary model of a m2m


field and no through_fields have been set, Django will show an error with
the following hint:
{{{#!python
hint=(
'If you want to create a recursive relationship, '
'use ForeignKey("%s", symmetrical=False, through="%s").'
}}}

But 'symmetrical' and 'through' are m2m keyword arguments, not ForeignKey.

This was probably a small mistake where the developer thought
ManyToManyField but typed ForeignKey instead. And the symmetrical=False is

an outdated requirement to recursive relationships with intermediary model
to self, not required since 3.0. I'll provide a PR with a proposed
correction shortly after.

Edit: fixed description.

--

--
Ticket URL: <https://code.djangoproject.com/ticket/31310#comment:2>

Django

unread,
Feb 27, 2020, 12:38:08 AM2/27/20
to django-...@googlegroups.com
#31310: Wrong hint about recursive relationship
-------------------------------------+-------------------------------------
Reporter: Matheus Cunha Motta | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Database layer | Version: 3.0
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Simon Charette):

* has_patch: 0 => 1
* stage: Unreviewed => Accepted


--
Ticket URL: <https://code.djangoproject.com/ticket/31310#comment:3>

Django

unread,
Feb 27, 2020, 5:46:43 AM2/27/20
to django-...@googlegroups.com
#31310: Wrong hint about recursive relationship.
-------------------------------------+-------------------------------------
Reporter: Matheus Cunha Motta | Owner: Matheus
Type: | Cunha Motta
Cleanup/optimization | Status: assigned
Component: Database layer | Version: master

(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 1 | Needs documentation: 0
Needs tests: 1 | Patch needs improvement: 0

Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* owner: nobody => Matheus Cunha Motta
* status: new => assigned
* version: 3.0 => master
* needs_tests: 0 => 1


--
Ticket URL: <https://code.djangoproject.com/ticket/31310#comment:4>

Django

unread,
Feb 28, 2020, 2:40:31 AM2/28/20
to django-...@googlegroups.com
#31310: Wrong hint about recursive relationship.
-------------------------------------+-------------------------------------
Reporter: Matheus Cunha Motta | Owner: Matheus
Type: | Cunha Motta
Cleanup/optimization | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by felixxm):

* needs_tests: 1 => 0
* stage: Accepted => Ready for checkin


--
Ticket URL: <https://code.djangoproject.com/ticket/31310#comment:5>

Django

unread,
Feb 28, 2020, 3:10:15 AM2/28/20
to django-...@googlegroups.com
#31310: Wrong hint about recursive relationship.
-------------------------------------+-------------------------------------
Reporter: Matheus Cunha Motta | Owner: Matheus
Type: | Cunha Motta
Cleanup/optimization | Status: assigned
Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Mariusz Felisiak <felisiak.mariusz@…>):

In [changeset:"308fab924195bdea2fc481e1466c3c3894c03400" 308fab92]:
{{{
#!CommitTicketReference repository=""
revision="308fab924195bdea2fc481e1466c3c3894c03400"
Refs #31310 -- Added test for check for using intermediate model with
ambiguous foreign key from model.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31310#comment:6>

Django

unread,
Feb 28, 2020, 3:10:15 AM2/28/20
to django-...@googlegroups.com
#31310: Wrong hint about recursive relationship.
-------------------------------------+-------------------------------------
Reporter: Matheus Cunha Motta | Owner: Matheus
Type: | Cunha Motta
Cleanup/optimization | Status: closed

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: fixed

Keywords: | Triage Stage: Ready for
| checkin
Has patch: 1 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 1 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Mariusz Felisiak <felisiak.mariusz@…>):

* status: assigned => closed
* resolution: => fixed


Comment:

In [changeset:"e908eb62871f0b0aac63afa6601bf222bc2a1a7d" e908eb6]:
{{{
#!CommitTicketReference repository=""
revision="e908eb62871f0b0aac63afa6601bf222bc2a1a7d"
Fixed #31310 -- Fixed hints in checks for using intermediate model with
ambiguous foreign key.

symmetrical=False is not required since
87b1ad6e7351464c60e751b483d9dfce3a2d3382.
}}}

--
Ticket URL: <https://code.djangoproject.com/ticket/31310#comment:7>

Reply all
Reply to author
Forward
0 new messages