Understanding the Many-To-Many Relationship Concept

Many to many relationships is one of the most fundamental and useful concepts to understand when it comes to development. What exactly does this mean? To illustrate it simply, a doctor has many patients and a patient has many or can have many doctors. Essentially, they are two models or classes with several instances that somehow need to communicate. How do they do this? Let’s break it down.

To connect our two classes we need to have a joiner. What can be that common ground for both our patient and doctor? Well, an appointment would fulfill this requirement. It’s like their meeting or intersection point.

Before we even start coding it’s extremely important to understand which are your has many classes and which is the joiner. I recommend drawing this out and making the connections clear beforehand.

This means we have to create a class of Appointments. Now the doctor has to go through the Appointments class to get any information about their patient and vice versa. How?

Our instances would then look something like this:

mock up instances for many-to-many relationship

As we can see, we have our list of patients with their age and doctors with their practice; and yes, those are in fact Grey’s Anatomy references!

What do you notice about our Appointment instances? They directly reference both the patient and doctor classes! For example, our first appointment is with our patient Evelyn and she will be seeing Dr. Grey from our Doctors instances. Her appointment is on Monday.

Now you’re ready to create methods that get your app or program up and running since through this Appointment class all of your classes are tied together.

As you create your methods, think about how this relationship is set up. Remember, every time you want your Doctor class to access your Patient class you will have to go through that joiner class, Appointments. The same goes for Patients to Doctors. This is the purpose of the joiner class. So you should see the Appointment class referenced somewhere in your method.

Above is an example for if you wanted to make a new appointment for your patient, a method called add_appointment in your Patient class. However, how can we tell our method with which doctor? If you’re guessing through the Appointment class, you’re absolutely right! Through this method we are able to make a new Appointment instance and at the same time tell our program which patient, reference the doctor from our Doctors instances, and tell it what day since the Appointment class holds this information.

There you have it. A broken down summary of the at first intimidating concept of many to many relationships. Happy coding!





Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Significance Testing and Confidence Intervals in Python with non-normal data

Android — Create Root Checker App in Just 10 minutes

Understanding Linking

BQSmartMigrate — BigQuery Schema & Data Migration Tool

How Quizlet uses Apache Airflow in practice

Book Review-Getting Results the Agile Way

Week I Developing Discoveries: Shared Libraries

✨NO.1 software development company in Hosur.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Alexa Botello

Alexa Botello


More from Medium

Are You Being Diligent in Clearly Living As Your Authentic Self?

Are you A Highly Sensitive Person?

N E V E R M I N D E R S nft