How to store users's notifications options?

送分小仙女□ 提交于 2020-07-21 19:43:08

问题


I'm creating a database users. I want to let users to choose notifications they want to receive by email.

Now I have the next columns in table users (boolean type):

  • notification_comment_photo.

  • notification_comment_comment.

  • notification_vote_photo.

  • notification_vote_comment.

  • notification_pm.

  • notification_followed.

  • notification_news.

What do you think, should I normalise table users and create another table notifications, considering that this table would have one-to-one relationship to table users?

Also I have the same problem with social links (twitter, facebook, google+, etc). Is it better to make a separate table links?

upd. Thanks all, I'll add the separate tables.


回答1:


It's hard to answer your question, because you're not telling us what problem you're trying to solve.

One issue with your current design is that it requires a schema change for every new type of notification you want to store - if you want to notify users when they've been un_followed, you have to add a column to your users table.

I'd consider a schema like:

TABLE: users
------------------
ID
...

TABLE: notification_types
----------------------
ID
Description

TABLE: user_notifcation_subscriptions
-----------------------------------------
user_id
notification_type_id
subscribed (bool)

You could leave the "subscribed" column out of user_notification_subscriptions and decide that any record linking a user to a notification type means they have subscribed.

This design allows you to add new subscription types without changing the schema. I believe it's similar to the design @Daniel suggests, but he doesn't include the notification_type table, relying instead on name-value pairs. I'm not a fan of this - it can lead to silly, hard-to-find bugs when typos slip into the TYPE column.




回答2:


You could (and probably should) create a separate table "notification_settings" or something.

 ID
 USER_ID
 TYPE
 VALUE

This allows you to easily add notification settings without messing with the database tables. Having a "strict" structure as you suggested sometimes gets in the way in the end and would be harder to expand.

For your social links, you should do the same. Another table named "user_social_accounts"

ID
USER_ID
NETWORK_ID


来源:https://stackoverflow.com/questions/12879935/how-to-store-userss-notifications-options

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