teacher-tools/api/schema/classroom_compliance.sql

45 lines
1.4 KiB
MySQL
Raw Normal View History

CREATE TABLE classroom_compliance_class (
2025-01-23 17:10:32 +00:00
id BIGSERIAL PRIMARY KEY,
number INT NOT NULL
CONSTRAINT class_number_check CHECK (number > 0),
school_year VARCHAR(9) NOT NULL,
user_id BIGINT NOT NULL
REFERENCES auth_user(id)
2025-01-24 00:08:16 +00:00
ON UPDATE CASCADE ON DELETE CASCADE,
CONSTRAINT unique_class_numbers_per_school_year
UNIQUE(number, school_year, user_id)
);
CREATE TABLE classroom_compliance_student (
2025-01-23 17:10:32 +00:00
id BIGSERIAL PRIMARY KEY,
name VARCHAR(255) NOT NULL,
class_id BIGINT NOT NULL
REFERENCES classroom_compliance_class(id)
ON UPDATE CASCADE ON DELETE CASCADE,
desk_number INT NOT NULL DEFAULT 0,
removed BOOLEAN NOT NULL DEFAULT FALSE
);
CREATE TABLE classroom_compliance_entry (
2025-01-23 17:10:32 +00:00
id BIGSERIAL PRIMARY KEY,
class_id BIGINT NOT NULL
REFERENCES classroom_compliance_class(id)
ON UPDATE CASCADE ON DELETE CASCADE,
student_id BIGINT NOT NULL
REFERENCES classroom_compliance_student(id)
ON UPDATE CASCADE ON DELETE CASCADE,
date DATE NOT NULL,
created_at BIGINT NOT NULL
DEFAULT EXTRACT(EPOCH FROM CURRENT_TIMESTAMP) * 1000,
absent BOOLEAN NOT NULL DEFAULT FALSE,
comment VARCHAR(2000) NOT NULL DEFAULT '',
phone_compliant BOOLEAN NULL DEFAULT NULL,
behavior_rating INT NULL DEFAULT NULL,
CONSTRAINT absence_nulls_check CHECK (
(absent AND phone_compliant IS NULL AND behavior_rating IS NULL) OR
(NOT absent AND phone_compliant IS NOT NULL AND behavior_rating IS NOT NULL)
2025-01-24 00:08:16 +00:00
),
CONSTRAINT unique_entry_per_date
UNIQUE(class_id, student_id, date)
);