如何在DB中设计USER与FAVORITE(或LIKE)模型之间的关系 [英] How to design the relations between USER and FAVORITE(or LIKE) models in DB
问题描述
我有这些模型(表): USER
,照片
, BOOK
,
QUESTION
等
现在我想添加收藏夹
或喜欢
功能到网站,这是用户可以喜欢一些照片或书籍或问题,当然,许多用户可以喜欢的照片。
所以我想问你如何实现这种事情。
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.
一个页面,显示当前登录所喜欢的图书,照片,quesitons等用户;
有多少人喜欢这本书。
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.
表: books_users
book_id user_id
book_id user_id
表: photos_users
photo_id user_id
photo_id user_id
表: questions_users
question_id user_id
question_id user_id
B
表: users_likes
object_id user_id type
列'type'设置为保存'book','photo'信息。
column 'type ' is set to hold the 'book', 'photo' infos.
A或B?
在OOP中真正困扰的是我们正在尝试在保持相同的对象之间创建一个继承关系。但是在关系数据库中,将事情保存在一个表格(计划B)中,或者保持分开(计划A)是很好的。我没有太多的DB设计经验,所以请告诉我的利弊?
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?
推荐答案
这使用超类型/子类型关系。有关更多类似的示例和说明,请访问 查看此处 以及如何 创建表 。
This uses supertype/subtype relationship. For a few more similar examples and explanations see here and how to create tables.
这篇关于如何在DB中设计USER与FAVORITE(或LIKE)模型之间的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!