fix: Update Notion sync logic to handle existing records and avoid unique constraint errors
This commit is contained in:
@@ -79,27 +79,20 @@ def sync_categories(token, session):
|
||||
|
||||
notion_id = page["id"]
|
||||
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"))
|
||||
key = name.lower().replace(" ", "_") if name else "unknown"
|
||||
|
||||
if not name: continue
|
||||
|
||||
# Upsert
|
||||
# Upsert Logic: Check ID -> Check Key -> Create
|
||||
cat = session.query(RoboticsCategory).filter(RoboticsCategory.notion_id == notion_id).first()
|
||||
if not cat:
|
||||
cat = session.query(RoboticsCategory).filter(RoboticsCategory.key == key).first()
|
||||
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)
|
||||
|
||||
@@ -125,9 +118,15 @@ def sync_industries(token, session):
|
||||
name = extract_title(props.get("Industry"))
|
||||
if not name: continue
|
||||
|
||||
# Upsert Logic: Check ID -> Check Name -> Create
|
||||
industry = session.query(Industry).filter(Industry.notion_id == notion_id).first()
|
||||
if not industry:
|
||||
industry = Industry(notion_id=notion_id)
|
||||
industry = session.query(Industry).filter(Industry.name == name).first()
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user