Student portal database schema
The tables behind the student portal, as SQL, as an ER diagram and as a Prisma schema.
A working student portal to start from
The database schema
Written by the free Database Schema Generator from a one-paragraph brief of the app.
The model centres on student, class_group and lesson: students belong to a class group, and each lesson is one teacher teaching one subject to one class group at a scheduled time. Attendance is recorded per student per lesson, grades per student per assignment, and sign-in accounts are separate from the people they link to.
Tables
app_user: one sign-in account, with a role (admin, office, teacher or student); the first account created becomes the admin.student: one enrolled student, optionally linked to their sign-in account and placed in a class group.teacher: one member of teaching staff, optionally linked to a sign-in account.class_group: one class (for example "7B") in a given school year, with an optional form teacher.subject: one subject taught at the school, such as Mathematics.lesson: one scheduled session of a subject, taught by a teacher to a class group at a specific date and time.attendance: one student's attendance status for one lesson.assignment: one gradable piece of work (test, homework, project) set for a subject and class group by a teacher.grade: one student's score on one assignment.announcement: one school-wide message posted by a user.
SQL (PostgreSQL)
CREATE TABLE app_user (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
email text NOT NULL UNIQUE,
password_hash text NOT NULL,
role text NOT NULL DEFAULT 'student' CHECK (role IN ('admin', 'office', 'teacher', 'student')),
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE teacher (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
user_id uuid UNIQUE REFERENCES app_user(id) ON DELETE SET NULL, -- signs in as
first_name text NOT NULL,
last_name text NOT NULL,
email text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE class_group (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
name text NOT NULL,
school_year text NOT NULL,
form_teacher_id uuid REFERENCES teacher(id) ON DELETE SET NULL, -- is form teacher of
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (name, school_year)
);
CREATE TABLE student (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
user_id uuid UNIQUE REFERENCES app_user(id) ON DELETE SET NULL, -- signs in as
class_group_id uuid REFERENCES class_group(id) ON DELETE SET NULL, -- enrols
first_name text NOT NULL,
last_name text NOT NULL,
date_of_birth date,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE subject (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
name text NOT NULL UNIQUE,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE lesson (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
class_group_id uuid NOT NULL REFERENCES class_group(id) ON DELETE CASCADE, -- is taught in
subject_id uuid NOT NULL REFERENCES subject(id) ON DELETE RESTRICT, -- is taught as
teacher_id uuid NOT NULL REFERENCES teacher(id) ON DELETE RESTRICT, -- teaches
starts_at timestamptz NOT NULL,
ends_at timestamptz NOT NULL,
room text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
CHECK (ends_at > starts_at)
);
CREATE TABLE attendance (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
lesson_id uuid NOT NULL REFERENCES lesson(id) ON DELETE CASCADE, -- is recorded for
student_id uuid NOT NULL REFERENCES student(id) ON DELETE CASCADE, -- attends
status text NOT NULL DEFAULT 'present' CHECK (status IN ('present', 'absent', 'late', 'excused')),
note text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (lesson_id, student_id)
);
CREATE TABLE assignment (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
subject_id uuid NOT NULL REFERENCES subject(id) ON DELETE RESTRICT, -- covers
class_group_id uuid NOT NULL REFERENCES class_group(id) ON DELETE CASCADE, -- is set for
teacher_id uuid NOT NULL REFERENCES teacher(id) ON DELETE RESTRICT, -- sets
title text NOT NULL,
due_date date,
max_points numeric(6,2) NOT NULL DEFAULT 100,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE TABLE grade (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
assignment_id uuid NOT NULL REFERENCES assignment(id) ON DELETE CASCADE, -- is graded in
student_id uuid NOT NULL REFERENCES student(id) ON DELETE CASCADE, -- earns
points numeric(6,2) NOT NULL,
comment text,
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now(),
UNIQUE (assignment_id, student_id)
);
CREATE TABLE announcement (
id uuid PRIMARY KEY DEFAULT gen_random_uuid(),
author_id uuid REFERENCES app_user(id) ON DELETE SET NULL, -- posts
title text NOT NULL,
body text NOT NULL,
published_at timestamptz NOT NULL DEFAULT now(),
created_at timestamptz NOT NULL DEFAULT now(),
updated_at timestamptz NOT NULL DEFAULT now()
);
CREATE INDEX lesson_class_group_starts_at_idx ON lesson (class_group_id, starts_at);
CREATE INDEX attendance_student_id_idx ON attendance (student_id);
CREATE INDEX grade_student_id_idx ON grade (student_id);
CREATE INDEX announcement_published_at_idx ON announcement (published_at DESC);
ERD
erDiagram
app_user {
uuid id PK
text email
text password_hash
text role
timestamptz created_at
timestamptz updated_at
}
teacher {
uuid id PK
uuid user_id FK
text first_name
text last_name
text email
timestamptz created_at
timestamptz updated_at
}
class_group {
uuid id PK
text name
text school_year
uuid form_teacher_id FK
timestamptz created_at
timestamptz updated_at
}
student {
uuid id PK
uuid user_id FK
uuid class_group_id FK
text first_name
text last_name
date date_of_birth
timestamptz created_at
timestamptz updated_at
}
subject {
uuid id PK
text name
timestamptz created_at
timestamptz updated_at
}
lesson {
uuid id PK
uuid class_group_id FK
uuid subject_id FK
uuid teacher_id FK
timestamptz starts_at
timestamptz ends_at
text room
timestamptz created_at
timestamptz updated_at
}
attendance {
uuid id PK
uuid lesson_id FK
uuid student_id FK
text status
text note
timestamptz created_at
timestamptz updated_at
}
assignment {
uuid id PK
uuid subject_id FK
uuid class_group_id FK
uuid teacher_id FK
text title
date due_date
numeric max_points
timestamptz created_at
timestamptz updated_at
}
grade {
uuid id PK
uuid assignment_id FK
uuid student_id FK
numeric points
text comment
timestamptz created_at
timestamptz updated_at
}
announcement {
uuid id PK
uuid author_id FK
text title
text body
timestamptz published_at
timestamptz created_at
timestamptz updated_at
}
app_user |o--o| teacher : "signs in as"
teacher |o--o{ class_group : "is form teacher of"
app_user |o--o| student : "signs in as"
class_group |o--o{ student : "enrols"
class_group ||--o{ lesson : "is taught in"
subject ||--o{ lesson : "is taught as"
teacher ||--o{ lesson : "teaches"
lesson ||--o{ attendance : "is recorded for"
student ||--o{ attendance : "attends"
subject ||--o{ assignment : "covers"
class_group ||--o{ assignment : "is set for"
teacher ||--o{ assignment : "sets"
assignment ||--o{ grade : "is graded in"
student ||--o{ grade : "earns"
app_user |o--o{ announcement : "posts"
Prisma
model AppUser {
id String @id @default(uuid()) @db.Uuid
email String @unique
passwordHash String @map("password_hash")
role String @default("student")
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
teacher Teacher?
student Student?
announcements Announcement[]
@@map("app_user")
}
model Teacher {
id String @id @default(uuid()) @db.Uuid
userId String? @unique @map("user_id") @db.Uuid
firstName String @map("first_name")
lastName String @map("last_name")
email String?
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
user AppUser? @relation(fields: [userId], references: [id], onDelete: SetNull)
classGroups ClassGroup[]
lessons Lesson[]
assignments Assignment[]
@@map("teacher")
}
model ClassGroup {
id String @id @default(uuid()) @db.Uuid
name String
schoolYear String @map("school_year")
formTeacherId String? @map("form_teacher_id") @db.Uuid
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
formTeacher Teacher? @relation(fields: [formTeacherId], references: [id], onDelete: SetNull)
students Student[]
lessons Lesson[]
assignments Assignment[]
@@unique([name, schoolYear])
@@map("class_group")
}
model Student {
id String @id @default(uuid()) @db.Uuid
userId String? @unique @map("user_id") @db.Uuid
classGroupId String? @map("class_group_id") @db.Uuid
firstName String @map("first_name")
lastName String @map("last_name")
dateOfBirth DateTime? @map("date_of_birth") @db.Date
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
user AppUser? @relation(fields: [userId], references: [id], onDelete: SetNull)
classGroup ClassGroup? @relation(fields: [classGroupId], references: [id], onDelete: SetNull)
attendance Attendance[]
grades Grade[]
@@map("student")
}
model Subject {
id String @id @default(uuid()) @db.Uuid
name String @unique
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
lessons Lesson[]
assignments Assignment[]
@@map("subject")
}
model Lesson {
id String @id @default(uuid()) @db.Uuid
classGroupId String @map("class_group_id") @db.Uuid
subjectId String @map("subject_id") @db.Uuid
teacherId String @map("teacher_id") @db.Uuid
startsAt DateTime @map("starts_at") @db.Timestamptz
endsAt DateTime @map("ends_at") @db.Timestamptz
room String?
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
classGroup ClassGroup @relation(fields: [classGroupId], references: [id], onDelete: Cascade)
subject Subject @relation(fields: [subjectId], references: [id], onDelete: Restrict)
teacher Teacher @relation(fields: [teacherId], references: [id], onDelete: Restrict)
attendance Attendance[]
@@index([classGroupId, startsAt])
@@map("lesson")
}
model Attendance {
id String @id @default(uuid()) @db.Uuid
lessonId String @map("lesson_id") @db.Uuid
studentId String @map("student_id") @db.Uuid
status String @default("present")
note String?
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
lesson Lesson @relation(fields: [lessonId], references: [id], onDelete: Cascade)
student Student @relation(fields: [studentId], references: [id], onDelete: Cascade)
@@unique([lessonId, studentId])
@@index([studentId])
@@map("attendance")
}
model Assignment {
id String @id @default(uuid()) @db.Uuid
subjectId String @map("subject_id") @db.Uuid
classGroupId String @map("class_group_id") @db.Uuid
teacherId String @map("teacher_id") @db.Uuid
title String
dueDate DateTime? @map("due_date") @db.Date
maxPoints Decimal @default(100) @map("max_points") @db.Decimal(6, 2)
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
subject Subject @relation(fields: [subjectId], references: [id], onDelete: Restrict)
classGroup ClassGroup @relation(fields: [classGroupId], references: [id], onDelete: Cascade)
teacher Teacher @relation(fields: [teacherId], references: [id], onDelete: Restrict)
grades Grade[]
@@map("assignment")
}
model Grade {
id String @id @default(uuid()) @db.Uuid
assignmentId String @map("assignment_id") @db.Uuid
studentId String @map("student_id") @db.Uuid
points Decimal @db.Decimal(6, 2)
comment String?
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
assignment Assignment @relation(fields: [assignmentId], references: [id], onDelete: Cascade)
student Student @relation(fields: [studentId], references: [id], onDelete: Cascade)
@@unique([assignmentId, studentId])
@@index([studentId])
@@map("grade")
}
model Announcement {
id String @id @default(uuid()) @db.Uuid
authorId String? @map("author_id") @db.Uuid
title String
body String
publishedAt DateTime @default(now()) @map("published_at") @db.Timestamptz
createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
author AppUser? @relation(fields: [authorId], references: [id], onDelete: SetNull)
@@index([publishedAt(sort: Desc)])
@@map("announcement")
}