Skip to content

Normalize the Player model by extracting Position, Team, and League into separate tables #125

Open
@nanotaboada

Description

@nanotaboada

Description

Refactor the Player model to follow database normalization practices. Extract Position, Team, and League into their own tables, reducing data redundancy and improving data integrity.

ERD

football-sqlite3-erd

DDL

CREATE TABLE IF NOT EXISTS "leagues" (
    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
    "name" TEXT NOT NULL UNIQUE,
    "country" TEXT NOT NULL,
    "confederation" TEXT
);

CREATE TABLE IF NOT EXISTS "teams" (
    -- https://liaison.reuters.com/tools/sports-team-codes
    "id" TEXT PRIMARY KEY,
    "name" TEXT NOT NULL UNIQUE,
    "leagueId" INTEGER NOT NULL,
    FOREIGN KEY ("leagueId") REFERENCES "leagues"("id") ON UPDATE NO ACTION ON DELETE CASCADE
);

CREATE TABLE IF NOT EXISTS "positions" (
    "id" TEXT NOT NULL PRIMARY KEY,
    "name" TEXT NOT NULL,
    CHECK (
        id = 'GK' AND name = 'Goalkeeper'
        OR id = 'RB' AND name = 'Right-Back'
        OR id = 'LB' AND name = 'Left-Back'
        OR id = 'CB' AND name = 'Centre-Back'
        OR id = 'DM' AND name = 'Defensive Midfield'
        OR id = 'CM' AND name = 'Central Midfield'
        OR id = 'RW' AND name = 'Right Winger'
        OR id = 'AM' AND name = 'Attacking Midfield'
        OR id = 'CF' AND name = 'Centre-Forward'
        OR id = 'SS' AND name = 'Second Striker'
        OR id = 'LW' AND name = 'Left Winger'
    )
);

CREATE TABLE IF NOT EXISTS "players" (
    "id" INTEGER PRIMARY KEY AUTOINCREMENT,
    "firstName" TEXT NOT NULL,
    "middleName" TEXT,
    "lastName" TEXT NOT NULL,
    "dateOfBirth" TEXT,
    "squadNumber" INTEGER NOT NULL UNIQUE,
    "positionId" TEXT NOT NULL,
    "teamId" TEXT NOT NULL,
    "starting11" BOOLEAN,
    FOREIGN KEY ("positionId") REFERENCES "positions"("id") ON UPDATE NO ACTION ON DELETE NO ACTION,
    FOREIGN KEY ("teamId") REFERENCES "teams"("id") ON UPDATE NO ACTION ON DELETE NO ACTION
);

CREATE INDEX IF NOT EXISTS "idx_teams_leagueId" ON "teams"("leagueId");
CREATE INDEX IF NOT EXISTS "idx_players_positionId" ON "players"("positionId");
CREATE INDEX IF NOT EXISTS "idx_players_teamId" ON "players"("teamId");

Acceptance Criteria

  • Create new tables for Position, Team, and League.
  • Modify the Player table to use foreign keys referencing the new tables.
  • Migrate existing data to match the new schema.
  • Write integration tests to verify proper data relationships.

Metadata

Metadata

Assignees

No one assigned

    Labels

    .NETPull requests that update .NET codeenhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions