diff --git a/blog/bp/blog/ghost/ghost_sync.py b/blog/bp/blog/ghost/ghost_sync.py index f1bd6fd..6bcc458 100644 --- a/blog/bp/blog/ghost/ghost_sync.py +++ b/blog/bp/blog/ghost/ghost_sync.py @@ -185,17 +185,25 @@ async def _upsert_post(sess: AsyncSession, gp: Dict[str, Any], author_map: Dict[ obj.user_id = user_id await sess.flush() - # rebuild post_authors + # rebuild post_authors (dedup to avoid composite PK conflicts from Ghost dupes) await sess.execute(delete(PostAuthor).where(PostAuthor.post_id == obj.id)) + await sess.flush() + seen_authors: set[int] = set() for idx, a in enumerate(gp.get("authors") or []): aa = author_map[a["id"]] - sess.add(PostAuthor(post_id=obj.id, author_id=aa.id, sort_order=idx)) + if aa.id not in seen_authors: + seen_authors.add(aa.id) + sess.add(PostAuthor(post_id=obj.id, author_id=aa.id, sort_order=idx)) - # rebuild post_tags + # rebuild post_tags (dedup similarly) await sess.execute(delete(PostTag).where(PostTag.post_id == obj.id)) + await sess.flush() + seen_tags: set[int] = set() for idx, t in enumerate(gp.get("tags") or []): tt = tag_map[t["id"]] - sess.add(PostTag(post_id=obj.id, tag_id=tt.id, sort_order=idx)) + if tt.id not in seen_tags: + seen_tags.add(tt.id) + sess.add(PostTag(post_id=obj.id, tag_id=tt.id, sort_order=idx)) # Auto-create PageConfig for pages if obj.is_page: