Entity relationships and modeling our database


Now that we have some understanding of how to describe a system, let's think a little bit more about the relationships between entities. For those of you who have not dealt with modeling a database before this will help you understand what fields you will need in our database. 

As an example let's consider a simplified baseball league. When we were describing systems in the previous task we could say that the following elements exist: leagues, teams, players, and games. What are the entity relationships between these? Let's start from the bottom and work our way up.

Player- Can only play for one team at a time. Example: Jose Reyes plays for the NY Mets

Team - Belongs to only 1 league. Has multiple players. Example: The NY Mets belong to the National League. They have a roster of n players on their team.

Leagues - Has multiple teams. Example: The NY Mets, Philidelphia Phillies, Washington Nationals belong to the National League

Games - Contains any 2 teams from either league. Examle: The NY Mets vs Washington Nationals

So what does this mean for building web applications?

  1. Player object would have a foreign key to a Team object
  2. Team object would have a foreign key to a League object
  3. Game object would have two fields that are foreign keys to a Team object
  4. Team object could have a Many to Many field with players

In django (and databases in general), a relationship between models is called a foreign key. These are really important for being able to get the data we want back from our database. 

Your assignment:

  1. Choose a system (maybe reuse the one you did for Task 2)
  2. Write down or diagram the entity relationships between the different elements
  3. Post them

For those that are confused on this topic please take a look at this excellent example. It goes into much greater detail than I just did. http://uswaretech.com/blog/2010/01/django-models-tutorial/

This can be kind of a tricky subject, but once you get it the next step in building our web application will be easy!

Task Discussion