[Answered ]-DB design for appointment scheduler

1👍

Yes, it looks like a good design. Don’t forget to declare a unique together constraint to avoid dups. Also, on TimeSlot, the student should be nullable to allow students to join a free timeSlot. Here it is:

  • Teacher:
    • id (pk)
    • name, …
  • Student:
    • id (pk)
    • name, …
  • TimeSlot:
    • id (pk)
    • teacher_id (not null, fk to teacher)
    • student_id (nullable #!Important , fk to student)
    • date (nut null)
    • starting_hour (nut null)
    • Constraint unique together #!Important: teacher_id, date, starting_hour.

On django you can declare it as many-to-many relationships with extra fields

from django.db import models

class Student(models.Model):
    name = models.CharField(max_length=128)

    def __str__(self):
        return self.name

class Teacher(models.Model):
    name = models.CharField(max_length=128)
    slots = models.ManyToManyField(Student, through='TimeSlot')

    def __str__(self):
        return self.name

class TimeSlot(models.Model):
    student = models.ForeignKey(
        Student,
        blank = True,  #!Important
        null =True,    #!Important
        on_delete=models.SET_NULL)
    teacher = models.ForeignKey(Teacher, on_delete=models.CASCADE)
    date = models.DateField()
    starting_hour = models.IntegerField(max_length=64)

class Meta:
    constraints = [
        models.UniqueConstraint( #!Important
            fields=['teacher', 'date', 'starting_hour'],
            name='unique one teacher by slot')
    ]

Leave a comment