fix: Update Notion sync logic to handle existing records and avoid unique constraint errors

This commit is contained in:
2026-01-19 11:37:55 +00:00
parent 694be6eb4d
commit 7c2ae08c74

View File

@@ -79,29 +79,22 @@ def sync_categories(token, session):
notion_id = page["id"] notion_id = page["id"]
name = extract_title(props.get("Name")) name = extract_title(props.get("Name"))
# In the inspected DB, there was no 'key' or 'description' obvious, checking props again:
# Properties: Constrains, Product Category, Text, Product Categories, Name
# Wait, the inspection output was:
# - Constrains (rich_text)
# - Product Category (relation)
# - Text (rich_text)
# - Product Categories (relation)
# - Name (title)
# It seems the schema might be slightly different than expected or I looked at the wrong DB.
# But 'Name' is there. I'll use Name as Key (lowercase) for now.
# And 'Text' as Description?
description = extract_rich_text(props.get("Text")) description = extract_rich_text(props.get("Text"))
key = name.lower().replace(" ", "_") if name else "unknown" key = name.lower().replace(" ", "_") if name else "unknown"
if not name: continue if not name: continue
# Upsert # Upsert Logic: Check ID -> Check Key -> Create
cat = session.query(RoboticsCategory).filter(RoboticsCategory.notion_id == notion_id).first() cat = session.query(RoboticsCategory).filter(RoboticsCategory.notion_id == notion_id).first()
if not cat: if not cat:
cat = RoboticsCategory(notion_id=notion_id, key=key) cat = session.query(RoboticsCategory).filter(RoboticsCategory.key == key).first()
session.add(cat) if cat:
logger.info(f"Linked existing category '{key}' to Notion ID {notion_id}")
cat.notion_id = notion_id
else:
cat = RoboticsCategory(notion_id=notion_id, key=key)
session.add(cat)
cat.name = name cat.name = name
cat.description = description cat.description = description
@@ -125,10 +118,16 @@ def sync_industries(token, session):
name = extract_title(props.get("Industry")) name = extract_title(props.get("Industry"))
if not name: continue if not name: continue
# Upsert Logic: Check ID -> Check Name -> Create
industry = session.query(Industry).filter(Industry.notion_id == notion_id).first() industry = session.query(Industry).filter(Industry.notion_id == notion_id).first()
if not industry: if not industry:
industry = Industry(notion_id=notion_id) industry = session.query(Industry).filter(Industry.name == name).first()
session.add(industry) if industry:
logger.info(f"Linked existing industry '{name}' to Notion ID {notion_id}")
industry.notion_id = notion_id
else:
industry = Industry(notion_id=notion_id, name=name)
session.add(industry)
# Map Fields # Map Fields
industry.name = name industry.name = name