Skip to content

Normalize the PlayerModel by extracting position, team, and league into separate tables #189

Open
@nanotaboada

Description

@nanotaboada

Description

Refactor the Player schema by normalizing related fields (position, team, and league) into separate tables. This will improve data integrity and reduce redundancy.

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 with the proposed DDL script.
  • Create new schemas for Position, Team, and League, defining relationships with the existing Player.
  • Create new PositionModel, TeamModel and LeagueModel.
  • Update existing the SQLAlchemy schemas, Pydantic models and FastAPI endpoints accordingly.
  • Migrate existing data without breaking API functionality.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestpythonPull requests that update Python code

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions