When you start beginning to use object oriented programming in Python the concept of many to many can seem very straightforward and dare I easy to understand. Or at least that's what I thought when I first was exposed to the concept. Actually I stand by that statement that it is an easy concept but will explain why this easy to understand concept is frustrating for programmers to understand. In this article we will get into this necessary topic when beginning object oriented programming. Why the concept isn't hard to understand but why it could be and when it becomes complicated.
What are Many to Many Relationships and Why Do We Use Them?
Object oriented programing is probably the first instance when you would be exposed to the phrase many to many but you should actually be familiar with the concept already. Human relationships are examples of many to many relationships. When there are many instances of a person having more than relationship title such as a person having two friends that are not friends or don't know each other. This is an example of a one to many relationship in the diagram below. Fred has many friends in this case named Jonny and Adam.
Many to many is similar except that we are focusing on the network of relationships that is formed by the interactions of many relationships. For example Fred that has two friends but the friends are friends with each other. This means that Fred has many friends but Jonny and Adam have many friends also.
How to Understand Many to Many Relationships?
The examples above are easy to understand yet it can still be confusing if you focus is on the object title instead of the relationship. Think of the examples above with friends. It is important to understand this concept by emphasizing the possessiveness of the word "having" and the connections being the focus not object titles. Visually you can think of the arrows being the focus of our attention because the arrows denote the relationship the objects themselves. In the examples the arrows have the label friend which is the relationship. However don't forget it is the objects that have the relationships.
Why Many to Many Relationships Get Complicated and What to Do About It
Conceptually many to many relationships are not the most complicated topic but understand that we are abstracting twice when working with many to many relationships. Once when thinking about relationships and the second when you are coding them. Because we are trying to hold two competing abstract concepts at the same time it can drain mental resources or even stump your brain. The good news is that once understanding the concept of many to many relationships becomes automatic it will free up your mind to code using these relationships.
If you are still having trouble understanding the concept, don't worry here are some additional tips to help you get the hang of it faster and easier.
Draw a picture like those in my examples. By drawing a picture we can remove one abstraction from our work and will only have to deal with one abstract concept, programming.
Think about how the relationships interact in real life. For instance you could have barns, animals and farmers. An cow animal could have just many barns that it frequents, the same cow could have many farmers and the farmers could have many barns. This in writing is somewhat abstract but if you think about the actual items or grab some toys to act out the relationships it will start to make sense.
Be patient. Abstract thinking is skill that can take time but if you give yourself time to adjust it will start to make sense. The programming aspect of many to many relationships especially can take lots of practice and time to master so start coding.
If you are still struggle its important to review and practice. You will get this concept eventually but with the examples above and your own many to many relationships will be easy a pi. Wait is pi easy? Good luck!


Top comments (0)