My Simple Guide to Database Relationships (From Confused to Clear π )

Heyyy guyyysss π€ (told you, you were gonna hear this a lot π)
I'm guessing you weren't expecting an article so soon, well I was working on a project in which I needed a table with a many-to-many relationship, then I realized, I don't fully understand how this relationship (database relationship ohh π) works.
So Iβm gonna explain the 3 database relationships. Personally, I understand things better when I can visualize them, so Iβll be dropping the tabular representation right below each Django model. These tables show you how the database actually stores the information.
1. One-to-One (1οΈβ£ β 1οΈβ£)
π Think of it like a person and their passport.
Each person can only have one passport.
Each passport belongs to one person only.
Example in Django:
class Person(models.Model):
name = models.CharField(max_length=100)
class Passport(models.Model):
number = models.CharField(max_length=20)
person = models.OneToOneField(Person, on_delete=models.CASCADE)
Tabular Representation
Person Table
| id | name |
| 1 | Alice |
| 2 | Bob |
Passport Table
| id | number | person |
| 1 | 1213331313 | 1 |
| 2 | 123232 | 2 |
Note: The person column in the passport table stores a number that is a reference to the ID in the person table. E.g., the row that stores person as 1 is referring to Alice in the person table. We do not want to hardcode the personβs name into the field as this is bad practice, so in subsequent tables, you will be seeing the ID stored in the row.
πOther usage includes:
Profile + User
Car + Engine
2. One-to-Many (1οΈβ£ β π) OR Many-to-One
π Think of it like a mother and her children.
One mother can have many children.
But each child has only one mother.
Example in Django:
class Mother(models.Model):
name = models.CharField(max_length=100)
class Child(models.Model):
name = models.CharField(max_length=100)
mother = models.ForeignKey(Mother, on_delete=models.CASCADE)
Tabular representation
Mother Table
| id | name |
| 1 | Ronny Anne |
| 2 | Don Cassy |
Child Table
| id | mother | name |
| 1 | 1 | Ronny janne |
| 2 | 1 | Ronny garty |
| 3 | 2 | Don Libert |
πOther usage includes:
Company β Employees
School β Student
3. Many-to-Many (π β π)
π Think of it like students and courses.
One student can enroll in many courses.
One course can have many students.
Example in Django:
class Student(models.Model):
name = models.CharField(max_length=100)
courses = models.ManyToManyField("Course", through="Enrollment")
class Course(models.Model):
title = models.CharField(max_length=100)
class Enrollment(models.Model): # the "through table"
student = models.ForeignKey(Student, on_delete=models.CASCADE)
course = models.ForeignKey(Course, on_delete=models.CASCADE)
date_enrolled = models.DateField()
grade = models.CharField(max_length=5, blank=True)
Tabular Representation
Student Table
| id | name |
| 1 | Alice |
| 2 | Bob |
| 3 | Chika |
Course Table
| id | title |
| 1 | Databases |
| 2 | Django Basics |
Enrollment Table
| id | student | course | date_enrolled | grade |
| 1 | 1 | 1 | 2025-01-10 | A |
| 2 | 2 | 1 | 2025-01-11 | B+ |
| 3 | 1 | 2 | 2025-02-01 | A- |
| 4 | 3 | 2 | 2025-02-03 | NULL |
πOther usage includes:
Doctors β Patients
Actors β Movies
At first, many-to-many confused me because of the extra join table, but once I visualized it as a separate table (Enrollment), it clicked. Which of these relationships do you find most confusing in your projects? Drop a comment, letβs discuss π

