Opened 3 years ago
Closed 3 years ago
#32983 closed Cleanup/optimization (fixed)
Prevent developers from defining a related_name on symmetrical ManyToManyFields
Reported by: | Nick Touran | Owned by: | Nick Touran |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 4.0 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
In ManyToManyField, if the symmetrical
argument is passed, or if it's a self-referential ManyToMany relationship, the related field on the target model is not created. However, if a developer passes in the related_name
not understanding this fact, they may be confused until they find the information about symmetrical relationship. Thus, it is proposed to raise an error when the user defines a ManyToManyField in this condition.
Change History (9)
comment:1 by , 3 years ago
Owner: | changed from | to
---|---|
Status: | new → assigned |
comment:3 by , 3 years ago
Patch needs improvement: | set |
---|---|
Triage Stage: | Unreviewed → Accepted |
OK, I guess we can do something here — it probably is a source of confusion.
The same issue was raised in #18021 (but as an invalid bug report, rather than suggesting improving the messaging).
Looking at the PR — I'm sceptical about just raising an error — this will likely break code in the wild.
Can we investigate adding a system check here instead?
There are several similar checks for related fields already: https://docs.djangoproject.com/en/3.2/ref/checks/#related-fields
comment:5 by , 3 years ago
Absolutely. A system check is a much better approach than my initial idea of the error. I have changed the patch to use a system check.
comment:6 by , 3 years ago
Patch needs improvement: | unset |
---|
Unchecking patch needs improvement as instructed on the page, (pending reviewer acceptance of course).
comment:7 by , 3 years ago
Triage Stage: | Accepted → Ready for checkin |
---|
I have a PR that implements this incoming.