简体   繁体   English

在数据库中保存友谊。 社交网络服务

[英]Saving friendship in the database. Social networking service

At present, I am currently storing in this way in the database 目前,我目前正以这种方式存储在数据库中

id  from_id  to_id
1     1        2
2     2        1

I write down such friendships in this way 我用这种方式写下这样的友谊

this.friendshipRepository.save(new FriendshipEntity(fromUser.get(), toUser.get()));
this.friendshipRepository.save(new FriendshipEntity(toUser.get(), fromUser.get()));

a when I remove 当我删除

this.friendshipRepository.delete(this.friendshipRepository.findOneByFromUserAndToUser(fromUser.get(), toUser.get()));
this.friendshipRepository.delete(this.friendshipRepository.findOneByFromUserAndToUser(toUser.get(), fromUser.get()));

I have to do everything twice. 我必须做两次。 It is a little inconvenient. 这有点不方便。

Do you know any better ways to store friendship between users in the database? 您知道在数据库中存储用户之间友谊的更好方法吗?

if you consider friendship is symmetric, then an option is to rewrite your database schema. 如果您认为友谊是对称的,则可以选择重写数据库架构。 instead of 代替

id  from_id  to_id

it could be 它可能是

id  lower_id  higher_id

so a given friendship has a unique entry in the database, and you can remove a friendship with a single query. 因此给定的友谊在数据库中具有唯一的条目,您可以通过单个查询删除友谊。

the downside is that if you want to list someone friends, you have to look up this someone in both lower_id and higher_id 缺点是,如果您想列出某人的朋友,则必须同时在 lower_idhigher_id查找此人

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM