Skip to main content

Command Palette

Search for a command to run...

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

Published
β€’3 min readβ€’View as Markdown
My Simple Guide to Database Relationships (From Confused to Clear πŸ˜…)
O
Software Engineer writing about cloud computing, backend engineering, AI engineering, and practical solutions to real-world technical problems.

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

idname
1Alice
2Bob

Passport Table

idnumberperson
112133313131
21232322

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

idname
1Ronny Anne
2Don Cassy

Child Table

idmothername
11Ronny janne
21Ronny garty
32Don 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

idname
1Alice
2Bob
3Chika

Course Table

idtitle
1Databases
2Django Basics

Enrollment Table

idstudentcoursedate_enrolledgrade
1112025-01-10A
2212025-01-11B+
3122025-02-01A-
4322025-02-03NULL

πŸ“Œ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 πŸ‘‡