Is there an elegant way to store a dual relationship (i.e. user 1 and user 2 are friends)

前端 未结 5 1099
野趣味
野趣味 2020-12-16 06:01

I\'ve run into the same problem in two different pieces of work this month:

Version 1: User 1 & User 2 are friends
Version 2: Axis 1 & Axis 2 when gr         


        
5条回答
  •  天命终不由人
    2020-12-16 06:56

    "x is a friend of y".

    Define a table of (x,y) pairs and enforce a canonical form, e.g. x

    Create a view as SELECT x,y FROM FRIENDS UNION SELECT x as y, y as x FROM FRIENDS.

    Do your updates against the base table (downside : updaters must be aware of the enforced canonical form), do your queries against the view.

提交回复
热议问题