-- chapter_sources: drop the global (source_id, source_chapter_key) PK -- and rekey on (source_id, chapter_id). -- -- The old PK assumed chapter slugs are unique per source. Sources whose -- chapter naming is per-manga (chapter-1, chapter-2, ...) instead of per- -- catalog (br_chapter-379272 with a global counter) would collide on the -- second manga: the INSERT would conflict on (source_id, "chapter-1") and -- the lookup would attribute the row to the first manga's chapter_id. -- -- The new key is the natural identity of a source attachment: "this source -- has this chapter". An (source_id, source_chapter_key) index preserves -- the lookup path (find existing source row by source's identifier) but -- no longer enforces uniqueness — the application combines it with the -- chapters table's manga_id to scope the lookup per-manga. ALTER TABLE chapter_sources DROP CONSTRAINT chapter_sources_pkey; ALTER TABLE chapter_sources ADD PRIMARY KEY (source_id, chapter_id); CREATE INDEX chapter_sources_source_key_idx ON chapter_sources (source_id, source_chapter_key);