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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.