#24289: Is usage of many_to_one and one_to_many terms confusing for relation
flags?
-------------------------------------+-------------------------------------
Reporter: akaariai | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 1.8alpha1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage: Accepted
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by carljm):
* status: closed => new
* resolution: invalid =>
* stage: Unreviewed => Accepted
Comment:
I am reopening this because we have, at the very least, an inconsistency
within Django itself. The documentation at
https://docs.djangoproject.com/en/dev/topics/db/examples/many_to_one/ says
that `ForeignKey` defines a many-to-one relationship, but an actual
`ForeignKey` claims to be one-to-many in the field flags. I suppose this
inconsistency could be justified by saying "yes, a ForeignKey -defines- a
many-to-one in the opposite direction", but I think that's a pretty
stretched justification.
Russell, I don't understand your claim that we have "a precedent that goes
back 10 years" here. We're discussing the `one_to_many` and `many_to_one`
field flags, which are new in Django 1.8. `git grep one_to_many` returns
zero results in the `stable/1.7.x` branch. If we have any 10-year
precedent here, it's in the documentation I linked, which uses the
opposite terminology from what we've now introduced in the field flags.
It does seem to me that usage is mixed: the IBM docs at
http://www-01.ibm.com/support/knowledgecenter/SSEPGG_8.2.0/com.ibm.db2.udb.doc/admin/c0004733.htm
do say that "the relationship between employees (single-valued) and
departments (multi-valued) is a one-to-many relationship" in a case where
Employee has an FK to Department. That would be consistent with our
current flags (but not with our docs).
But my searching seems to indicate that the opposite usage is much more
common in practice:
- http://en.wikipedia.org/wiki/Foreign_key says that "the relationship
between the two tables is called a one to many relationship between the
referenced table and the referencing table" (note that the table with the
FK is the "referencing" table, so this is saying that a forward FK is
many-to-one and the reverse is one-to-many.)
- http://www.databaseprimer.com/pages/relationship_1tox/ says that "In a
one-to-many relationship between Table A and Table B, each row in Table A
is linked to 0, 1 or many rows in Table B." Again, this would indicate
that an FK is many-to-one.
- The accepted answer at http://stackoverflow.com/questions/16119531
/hibernate-jpa-manytoone-vs-onetomany indicates that in Hibernate, an FK
is `ManyToOne` and the reverse relationship is `OneToMany`.
On the whole, it seems to me that we should go with the more common (and,
IMO, more intuitive) usage, which is also what's been in our docs for
quite a long time. This would imply reversing the current field flags, so
that a `ForeignKey` becomes `many_to_one`. But I certainly can't point to
conclusive evidence that this is correct.
--
Ticket URL: <https://code.djangoproject.com/ticket/24289#comment:5>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
--
You received this message because you are subscribed to the Google Groups
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/django-updates/066.7516598280c5898d26b7ebb107232558%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.