Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions backend/src/db/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -109,4 +109,42 @@ CREATE TABLE IF NOT EXISTS users (
COLLATE ascii_general_ci
NOT NULL,
UNIQUE INDEX idx_users_username (username)
);

-- Saved teams created by users.
CREATE TABLE IF NOT EXISTS saved_teams (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
user_id INT NOT NULL,
name VARCHAR(100) NOT NULL,
format VARCHAR(50) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
ON UPDATE CURRENT_TIMESTAMP,

INDEX idx_saved_teams_user_id (user_id),

FOREIGN KEY (user_id)
REFERENCES users(id)
ON DELETE CASCADE
);

-- Pokémon belonging to saved teams, with their original order.
CREATE TABLE IF NOT EXISTS saved_team_pokemon (
team_id INT NOT NULL,
pokemon_id VARCHAR(50) NOT NULL,
position TINYINT UNSIGNED NOT NULL,

PRIMARY KEY (team_id, pokemon_id),
UNIQUE KEY uq_saved_team_position (team_id, position),

CONSTRAINT chk_saved_team_position
CHECK (position BETWEEN 1 AND 12),

FOREIGN KEY (team_id)
REFERENCES saved_teams(id)
ON DELETE CASCADE,

FOREIGN KEY (pokemon_id)
REFERENCES pokemon(id)
ON DELETE RESTRICT
);
Loading