Opened 5 years ago
Last modified 5 years ago
#31310 closed Cleanup/optimization
Wrong hint about recursive relationship — at Initial Version
Reported by: | Matheus Cunha Motta | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | dev |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Ready for checkin | |
Has patch: | yes | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | yes | UI/UX: | no |
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:
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.