-- 同一对 (followed_id, fans_id) 只允许一行，避免并发/重复调用产生多条有效关注，
-- 与应用层 revive + INSERT 兜底一致。
-- 需要 MySQL 8+（窗口函数）。
-- 若 UNIQUE KEY uk_life_fans_follow_pair 已存在请勿重复执行 ADD，必要时先 DROP。

DELETE FROM life_fans
WHERE id IN (
    SELECT id FROM (
                       SELECT lf.id AS id,
                              ROW_NUMBER() OVER (
                                      PARTITION BY lf.followed_id, lf.fans_id
                                          ORDER BY CASE WHEN lf.delete_flag = 0 THEN 0 ELSE 1 END, lf.id ASC
                                  )       AS rn
                       FROM life_fans lf
                   ) ranked
    WHERE ranked.rn > 1
);

ALTER TABLE life_fans
    ADD UNIQUE KEY uk_life_fans_follow_pair (followed_id, fans_id);
