stooicrealism
stooicrealism

Reputation: 558

Implementing a database table to record two way user following system

The two tables here are :

user_table
--user_id (int)
--user_name (int) --user_profile_link (varchar(max)) --user_joined_date (smalldatetime) --user_thumbnail_pic (varbinary(MAX))

follower_table
--follow_id
--followed_user_id ( who i am following )
--following_user_id ( who i am )
--followed_mutual ( default : NO ) -- to identify if both are following each other

is this approach correct ? or do i need something else to make this tick ! need some advice on this guys...thanks

Upvotes: 0

Views: 62

Answers (2)

lloydom
lloydom

Reputation: 387

The follower table is a many to many table. So you dont need to have this field followed_mutual ( default : NO ) , since effectively you can query this table by doing a self join to find if they are mutual followers. I would also suggest that the user thumbnail field user_thumbnail_pic (varbinary(MAX)) in the usertable be put in a seperate table so that your queries are faster and that way it wont involve in queries that are more frequent.

Upvotes: 1

Sergio Tulentsev
Sergio Tulentsev

Reputation: 230316

I (personally) would create separate one-way relationships. If user 12 follows user 37 and it's mutual, then follower_table will have two records

user   following
  12       37
  37       12

Also, I would go ahead and duplicate reverse relationships

user   followed_by
  37       12
  12       37

This makes queries easier, and also greatly simplifies sharding of data, should you ever need to split your data across multiple DB servers.

Upvotes: 1

Related Questions