Fix post_authors duplicate key during Ghost sync
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 8m14s
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 8m14s
Add explicit flush after DELETE and dedup authors/tags to prevent autoflush-triggered IntegrityError on composite PK. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -185,17 +185,25 @@ async def _upsert_post(sess: AsyncSession, gp: Dict[str, Any], author_map: Dict[
|
|||||||
obj.user_id = user_id
|
obj.user_id = user_id
|
||||||
await sess.flush()
|
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.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 []):
|
for idx, a in enumerate(gp.get("authors") or []):
|
||||||
aa = author_map[a["id"]]
|
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.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 []):
|
for idx, t in enumerate(gp.get("tags") or []):
|
||||||
tt = tag_map[t["id"]]
|
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
|
# Auto-create PageConfig for pages
|
||||||
if obj.is_page:
|
if obj.is_page:
|
||||||
|
|||||||
Reference in New Issue
Block a user