问题描述
这是由此处另一个问题的后果引起的问题:有两个单独的用户表更好还是一个更好?
This is a question that arose from the consequences of another question here: Is it better to have two separate user tables or one?
假设我有两种类型的用户,作者和读者,每种用户都存储在以主帐户表为键的关系表中,如下所示:
Assuming I have two types of users, an Author and a Reader, each stored in relational tables keyed to a main Accounts table like so:
TABLE Accounts {
id
email
password
salt
created
modified
}
TABLE reader {
id
user_id
...
}
TABLE author {
id
user_id
...
}
当作者发布博客时,我应该使用 Authors 表中的唯一 id 还是 Accounts 表中的唯一 id 来标记博客?读者评论也是如此 - 我应该使用读者表唯一 ID 或帐户表唯一 ID 标记评论吗?
When an Author posts a blog, should I tag the blog with the unique id from the Authors table, or the unique id from the Accounts table? Same goes with reader comments - should I tag the comment with the Reader table unique id, or the Account table unique id?
所以,基本上要么:
TABLE Blogs {
id
author_id
}
或
TABLE Blogs {
id
account_id
}
哪个不太可能在以后反击?
Which is less likely to bite back later on?
推荐答案
TABLE user {
id
email
password
salt
created
modified
}
TABLE profile {
user_id
age
favorite_movie
... other useless stuff...
}
TABLE user_role {
user_id
role_id
}
TABLE role {
id
name (author, admin, user, subscriber, etc...)
}
TABLE blog {
id
user_id
title
text
...etc...
}
user HASMANY role
user HASONE profile
user HASONE blog
因此用户可以是管理员和作者.您可以通过查找找到他们的博客用于此 user_id 的匹配博客.如果您有帐户类型相关字段然后将它们全部放入配置文件"表中.
So a user can be an admin, and an author. You can find their blogs by lookingfor a matching blog for this user_id. If you have account type dependant fieldsthen place them all in the "profile" table.
这篇关于用于博客和评论的唯一 ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!