This course will become read-only in the near future. Tell us at community.p2pu.org if that is a problem.

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


  • dharol   Oct. 2, 2012, 4:35 p.m.

    System: Enterprise Reporting Software

    1. Project Managers (PMs)
    2. Clients
    3. Reports
    4. Report Elements
    5. Report Type
    6. Each report instance has a FK to a client and possibly a PM.

    Each Client has a FK to a PM, or possibly a OneToMany with multiple PMs.

    Each Report is created by and FK'd to a PM

    A Report can be saved and run again, so report instances would be FK'd to a Report Type, which contains certain Report Elements (data and charts to be included).

  • dharol   Oct. 2, 2012, 4:23 p.m.

    http://uswaretech.com/blog/2010/01/django-models-tutorial/ is broken :'(

  • Jessica Ledbetter   July 12, 2011, 5:29 p.m.

    Reddit:

    Post has a foreign key to a User object  

    a post is created by one User but a User can have many posts

    Post has many Votes (but only one Vote per user per post)

    Post has foreign key to a Category object

    Comment has a foreign key to a User object

  • Anonym   July 9, 2011, 8:30 p.m.

    Can we use mindmapping software to help us?

  • Alex Kehayias   July 9, 2011, 8:36 p.m.
    In Reply To:   Anonym   July 9, 2011, 8:30 p.m.

    Yes whatever you want to use to help map it out

  • Alex Kehayias   July 9, 2011, 8:36 p.m.
    In Reply To:   Anonym   July 9, 2011, 8:30 p.m.

    Yes whatever you want to use to help map it out

  • Anonym   July 9, 2011, 9:22 p.m.
    In Reply To:   Alex Kehayias   July 9, 2011, 8:36 p.m.

    Thanks