Enes
Enes

Reputation: 341

How to model tournaments database into a SQL in django

I want to model a tournament database to store data of online games My question is: How to create a model in relationship database to store all this types of tournaments? (such as, league of legends tournament, dota 2 tournament) For example, a tournament can have 8 teams or 5 teams.

This is the sketch I created in my mind. What things do you suggest (especially I need help with relationships of tables). Also how to keep team 1 and team 2 in the match table (such as, scores, winner, loser)

i thought; Game database

game_id,name

Player database

player_id,name,surname,country,Game(FK).. ( and some other fields)

Team database

team_id,name,country,game,Player(ManyToMany).. ( and some other fields)

Match database

match_id,name,match_game,match_map,team1,team2,winner,loser,date,duration,score1,score2.. ( and some other fields)

Tournament database

tournament_id,tournament_name,tournament_game,Match(ManyToMany).. ( and some other fields)

Upvotes: 2

Views: 933

Answers (1)

axevalley
axevalley

Reputation: 335

You can create something like this in [app_name]/models.py

from django.db import models


class Tournament(models.Model):
    name = models.CharField(max_length=255)


class Team(models.Model):
    name = models.CharField(max_length=255)


class Player(models.Model):
    first_name = models.CharField(max_length=255)
    last_name = models.CharField(max_length=255)
    country = models.CharField(max_length=255)
    team = models.ForeignKey(Team, on_delete=models.CASCADE)


class Match(models.Model):
    name = models.CharField(max_length=255)
    match_game = models.CharField(max_length=255)
    match_map = models.CharField(max_length=255)
    match_teams = models.ManyToManyField(Team)
    winner = models.ForeignKey(Team, on_delete=models.CASCADE)
    loser = models.ForeignKey(Team, on_delete=models.CASCADE)
    duration = models.DurationField()
    winning_score = models.PositiveIntegerField()
    losing_score = models.PositiveIntegerField()
    tournament = models.ForeignKey(Tournament, on_delete=models.CASCADE)


class Game(models.Model):
    name = models.CharField(max_length=255)
    match = models.ForeignKey(Match, on_delete=models.CASCADE)

Some things to note:

  • You do not need to create ID fields, Django does this for you automatically.
  • A Many-to-Many field can often be replaced with a One-to-One field on the other model, for instance instead of many matches having many games, each game is part of one match. This may or may not work in your particular use case.
  • I have changed some field names (such as score_1 being replaced with winning_score) because I feel they are more clear, assuming I have correctly understood their purpose.
  • There are some fields (Tournament.tournament_game, Player.country) for which I used CharField but would be better served with a ForeingKey field to a separate model.

This also assumes that you do not need different fields for different types of tournament (League of Legends, DOTA). If you do need this you could achieve it with different models that inherit from an abstract base class:

class Game(models.Model):
    name = models.CharField(max_length=255)
    match = models.ForeignKey(Match, on_delete=models.CASCADE)

    class Meta:
        abstract = True


class DOTA2Game(Game):
    dota_field = models.CharField(max_length=255)


class LeagueOfLegendsGame(Game):
    lol_field = models.CharField(max_length=255)

In this example DOTA2Game and LeagueOfLegendsGame both inherit from Game and therefore have both a name and a match field as well as their custom fields. Setting abstract = True in the meta class of Game prevents it existing as a separate table within the database.

Upvotes: 2

Related Questions