def unfollow(cls, from_user_id, to_user_id): if from_user_id == to_user_id: return 0 if not GateKeeper.is_switched_on('switch_friendship_to_hbase'): # https://docs.djangoproject.com/en/3.1/ref/models/querysets/#delete # Queryset 的 delete 操作返回两个值,一个是删了多少数据,一个是具体每种类型删了多少 # 为什么会出现多种类型数据的删除?因为可能因为 foreign key 设置了 cascade 出现级联 # 删除,也就是比如 A model 的某个属性是 B model 的 foreign key,并且设置了 # on_delete=models.CASCADE, 那么当 B 的某个数据被删除的时候,A 中的关联也会被删除。 # 所以 CASCADE 是很危险的,我们一般最好不要用,而是用 on_delete=models.SET_NULL # 取而代之,这样至少可以避免误删除操作带来的多米诺效应。 deleted, _ = Friendship.objects.filter( from_user_id=from_user_id, to_user_id=to_user_id, ).delete() return deleted instance = cls.get_follow_instance(from_user_id, to_user_id) if instance is None: return 0 HBaseFollowing.delete(from_user_id=from_user_id, created_at=instance.created_at) HBaseFollower.delete(to_user_id=to_user_id, created_at=instance.created_at) return 1
def unfollow(cls, from_user_id, to_user_id): if from_user_id == to_user_id: return 0 # update Redis cls.remove_following_id_in_redis(from_user_id, to_user_id) # update DB if not GateKeeper.is_switch_on('switch_friendship_to_hbase'): deleted, _ = Friendship.objects.filter( from_user_id=from_user_id, to_user_id=to_user_id, ).delete() return deleted instance = cls.get_follow_instance(from_user_id, to_user_id) if instance is None: return 0 HBaseFollowing.delete(from_user_id=from_user_id, created_at=instance.created_at) HBaseFollower.delete(to_user_id=to_user_id, created_at=instance.created_at) return 1