Files
rust-imager/migrations/003_metadata_fix.sql
lychang d958655c1e update
2026-07-20 02:19:54 +08:00

20 lines
930 B
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- 别名表增加唯一约束,防止同一个人名下同名别名重复
--
-- 注意:需要先清理重复数据,再创建索引。
-- 如果先 CREATE UNIQUE INDEX,已有重复行时会直接报错。
-- 1. 清理可能存在的重复别名(保留 id 最小的那条)
DELETE FROM person_aliases WHERE id NOT IN (
SELECT MIN(id) FROM person_aliases GROUP BY person_id, alias_name
);
-- 2. 创建唯一索引(此时已无重复数据,不会失败)
CREATE UNIQUE INDEX IF NOT EXISTS idx_aliases_unique ON person_aliases(person_id, alias_name);
-- 相册关联表上增加唯一索引(已有 PRIMARY KEY,添加显式唯一索引加速)
-- 同时增加每张专辑只能有一个 person 的约束
-- 先清理旧的重复关联(同一album_id关联了多个person_id的情况)
DELETE FROM person_albums WHERE rowid NOT IN (
SELECT MIN(rowid) FROM person_albums GROUP BY album_id, person_id
);