DEV Community

ilija
ilija

Posted on • Updated on

Database relationships: many-to-one, one-to-one and many-to-many

In the database world, naming can be a bit confusing. For example many-to-one relationship and from opposite perspective one-to-many (often associated with ForeignKey in Django) miss a bit of consistency which can cause some confusion. I will try to offer here formal way to define 3 main relationships between database entities that can serve as a simple checker which can help us to determine the nature of relationship between two entities in our db.

Problem: when we say many-to-one (right side in bellow picture) and one-to-many (left side on picture) we lack certain language consistency. Because in the case of many-to-one perspective we are saying many instances on the right can have relation with only one instance of another entity on the left. So far so good. But if we take the opposite perspective, strict language usage should force us to say many instances on the left can have many relationships with entities on the right (many-to-many). And not as current convention is saying: one instance on the left can have relationships to many instances on the right (one-to-many).

Image description
Here proposed formalization always looks from the perspective of individual instance (which can be usefule when we design our db):

1) if one-to-one and one-to-many then relationships is many-to-one (ForeignKey)

2) if one-to-one and one-to-one then relationships is one-to-one 

3) if one-to-many and one-to-many then relationship is many-to-many 

Top comments (0)