How to design the relations between USER and FAVORITE(or LIKE) models in DB

喜你入骨 提交于 2019-12-02 16:38:11

问题


I have these models(tables): USER, PHOTO, BOOK, QUESTION etc. And now I wanna add the favorite or like feature to the site, that is user can 'like' some photos or books or questions, and of course a photo can be liked by many users. So I wanna ask you how to implement this kind of thing.

a page to show the books, photos, quesitons etc which are liked by the current login user; how many people like the book.

Here is my thoughts, give it simple.

A

table: books_users

book_id user_id

table: photos_users

photo_id user_id

table: questions_users

question_id user_id

B

table: users_likes

object_id user_id type

column 'type ' is set to hold the 'book', 'photo' infos.

A or B?

What really bothers me is in OOP we're trying to create a inheritence relationship between the objects that hold the same. But in relational databse, is that good to keep the things in one table(plan B), or keep them seperate(plan A).I don't have too much experiences in DB design so would you please tell the me pros and cons?


回答1:


This uses supertype/subtype relationship. For a few more similar examples and explanations see here and how to create tables.



来源:https://stackoverflow.com/questions/3350489/how-to-design-the-relations-between-user-and-favoriteor-like-models-in-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!