![]() ![]() The foreign keys in this table are both pointing at entries in the user table, since it is linking users to users. The followers table is the association table of the relationship. Here is a diagram of the self-referential many-to-many relationship that keeps track of followers: A relationship in which instances of a class are linked to other instances of the same class is called a self-referential relationship, and that is exactly what I have here. The second entity of the relationship is also the users. So what is the second entity of the many-to-many relationship? But in the case of followers, I have users following other users, so there is just users. In the students and teachers example I had two entities that were related through the many-to-many relationship. Looking at the summary of all the relationship types, it is easy to determine that the proper data model to track followers is the many-to-many relationship, because a user follows many users, and a user has many followers. While there are cases in which this type of relationship is useful, it isn't as common as the other types. ![]() The representation is similar, but a constraint is added to the database to prevent the "many" side to have more than one link. The difference is that this relationship is looked at from the "many" side.Ī one-to-one relationship is a special case of a one-to-many. ![]() Many-to-One and One-to-OneĪ many-to-one is similar to a one-to-many relationship. While it may not seem obvious at first, the association table with its two foreign keys is able to efficiently answer all the queries about the relationship. Here is how the database would look for the students and teachers example: The representation of a many-to-many relationship requires the use of an auxiliary table called an association table. This is actually non-trivial to represent in a relational database, as it cannot be done by adding foreign keys to the existing tables. It's like two overlapped one-to-many relationships from both ends.įor a relationship of this type I should be able to query the database and obtain the list of teachers that teach a given student, and the list of students in a teacher's class. I can say that a student has many teachers, and a teacher has many students. As an example, consider a database that has students and teachers. Many-to-ManyĪ many-to-many relationship is a bit more complex. The user_id field in the posts table is also sufficient to answer this question, as databases have indexes that allow for efficient queries such us "retrieve all posts that have a user_id of X". It is pretty clear that the user_id field provides direct access to the author of a given post, but what about the reverse direction? For the relationship to be useful I should be able to get the list of posts written by a given user. This field links each post to the record of its author in the user table. In the relationship above, the foreign key is the user_id field added to the posts table. The relationship is represented in the database with the use of a foreign key on the "many" side. I say that a user has many posts, and a post has one user (or author). The two entities linked by this relationship are users and posts. Here is the diagram for this relationship: I have already used a one-to-many relationship in Chapter 4. ![]() This is a good time to review the basic database relationship types: One-to-Many The database has a table that represents users, so what's left is to come up with the proper relationship type that can model the follower/followed link. Unfortunately, a relational database does not have a list type that I can use for these lists, all there is are tables with records and relationships between these records. I said above that I want to maintain a list of "followed" and "follower" users for each user. The GitHub links for this chapter are: Browse, Zip, Diff. So I'm going to be expanding the database so that it can keep track of who is following who, which is trickier than you may think. I want users of the application to be able to easily choose which other users they want to follow. In this chapter I am going to work on the application's database some more. Chapter 23: Application Programming Interfaces (APIs).Chapter 19: Deployment on Docker Containers.Chapter 15: A Better Application Structure.This is the eighth installment of the Flask Mega-Tutorial series, in which I'm going to tell you how to implement a "followers" feature similar to that of Twitter and other social networks.įor your reference, below is a list of the articles in this series. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |