brett
brett

Reputation: 351

follower/following relationship

I understand how basic database relationships work but I am confused about the following/follower social media relationship in RDBMs.

Users table:

userId (PK)
email
name

Follow table:

followerId (FK referencing userId),
followeeId (FK referencing userId)

This seems to be the correct database design but what I really want to know if this is a one to many or a many to many relationship.

I think it is a many to many because a user can follow many users and be followed by many users. But it may be a one to many because each follow a user makes consists of one userId to follow. This isn't the perfect many to many example such as Students and Classes so it is confusing me. Thanks.

Upvotes: 0

Views: 2507

Answers (1)

GMB
GMB

Reputation: 222402

It looks to me like your database design is consistent with your use case.

You have many-to-many relation between users, where a user may :

  • follow many users
  • AND be followed by many users

To represent this relationship, you have created a bridge table, the follow table. This is the relevant way to proceed.

The difference with a the typical Student/Class relation is that both objects being in relation are stored in the same table (users) : this is why you end up with two foreign keys in the follow table referencing user.userId. But regardless of this specifity, this is conceptually the same type of relationship.

Upvotes: 2

Related Questions