mirror of
https://github.com/OHTU-German-learning-website/OHTU-German-learning-website.git
synced 2026-10-12 19:13:51 +03:00
* fix * update requests * fixes * ssl * fix test * remove pgbouncer * cleanup
65 lines
2.1 KiB
SQL
65 lines
2.1 KiB
SQL
CREATE TABLE exercises (
|
|
id bigint PRIMARY KEY NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now()
|
|
);
|
|
|
|
CREATE TRIGGER updated_at
|
|
BEFORE UPDATE
|
|
ON public.exercises
|
|
FOR EACH ROW
|
|
EXECUTE FUNCTION updated_at();
|
|
|
|
-- Table holding all the possible words user can drag and drop and the correct answers
|
|
CREATE TABLE draggable_words (
|
|
id bigint PRIMARY KEY NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
word TEXT NOT NULL,
|
|
correct_answer TEXT NOT NULL,
|
|
UNIQUE (word, correct_answer),
|
|
UNIQUE (id, correct_answer)
|
|
);
|
|
|
|
CREATE TRIGGER updated_at
|
|
BEFORE UPDATE
|
|
ON public.draggable_words
|
|
FOR EACH ROW
|
|
EXECUTE FUNCTION updated_at();
|
|
|
|
-- Table holding the user's answers
|
|
CREATE TABLE user_answers (
|
|
id bigint PRIMARY KEY NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
user_id BIGINT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
draggable_words_id BIGINT NOT NULL REFERENCES draggable_words(id) ON DELETE CASCADE,
|
|
answer TEXT NOT NULL,
|
|
correct_answer TEXT NOT NULL,
|
|
is_correct BOOLEAN NOT NULL GENERATED ALWAYS AS (answer = correct_answer) STORED,
|
|
FOREIGN KEY (draggable_words_id, correct_answer) REFERENCES draggable_words(id, correct_answer) ON UPDATE CASCADE ON DELETE CASCADE
|
|
);
|
|
|
|
-- Table that joins the correct answer table and exercise table
|
|
CREATE TABLE words_to_exercises (
|
|
id bigint PRIMARY KEY NOT NULL GENERATED BY DEFAULT AS IDENTITY,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
updated_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
word_id bigint,
|
|
exercise_id bigint,
|
|
FOREIGN KEY (word_id) REFERENCES draggable_words(id),
|
|
FOREIGN KEY (exercise_id) REFERENCES exercises(id)
|
|
);
|
|
|
|
CREATE TRIGGER updated_at
|
|
BEFORE UPDATE
|
|
ON public.words_to_exercises
|
|
FOR EACH ROW
|
|
EXECUTE FUNCTION updated_at();
|
|
|
|
CREATE TRIGGER updated_at
|
|
BEFORE UPDATE
|
|
ON public.user_answers
|
|
FOR EACH ROW
|
|
EXECUTE FUNCTION updated_at();
|