@@ -422,34 +422,47 @@ async def update_entity_relations(
422422 # Clear existing relations first
423423 await self .relation_repository .delete_outgoing_relations_from_entity (db_entity .id )
424424
425- # Process each relation
426- for rel in markdown .relations :
427- # Resolve the target permalink
428- target_entity = await self .link_resolver .resolve_link (
429- rel .target ,
430- )
431-
432- # if the target is found, store the id
433- target_id = target_entity .id if target_entity else None
434- # if the target is found, store the title, otherwise add the target for a "forward link"
435- target_name = target_entity .title if target_entity else rel .target
436-
437- # Create the relation
438- relation = Relation (
439- from_id = db_entity .id ,
440- to_id = target_id ,
441- to_name = target_name ,
442- relation_type = rel .type ,
443- context = rel .context ,
444- )
445- try :
446- await self .relation_repository .add (relation )
447- except IntegrityError :
448- # Unique constraint violation - relation already exists
449- logger .debug (
450- f"Skipping duplicate relation { rel .type } from { db_entity .permalink } target: { rel .target } "
425+ # Batch resolve all relation targets in parallel
426+ if markdown .relations :
427+ import asyncio
428+
429+ # Create tasks for all relation lookups
430+ lookup_tasks = [
431+ self .link_resolver .resolve_link (rel .target ) for rel in markdown .relations
432+ ]
433+
434+ # Execute all lookups in parallel
435+ resolved_entities = await asyncio .gather (* lookup_tasks , return_exceptions = True )
436+
437+ # Process results and create relation records
438+ for rel , resolved in zip (markdown .relations , resolved_entities ):
439+ # Handle exceptions from gather and None results
440+ target_entity : Optional [Entity ] = None
441+ if not isinstance (resolved , Exception ):
442+ # Type narrowing: resolved is Optional[Entity] here, not Exception
443+ target_entity = resolved # type: ignore
444+
445+ # if the target is found, store the id
446+ target_id = target_entity .id if target_entity else None
447+ # if the target is found, store the title, otherwise add the target for a "forward link"
448+ target_name = target_entity .title if target_entity else rel .target
449+
450+ # Create the relation
451+ relation = Relation (
452+ from_id = db_entity .id ,
453+ to_id = target_id ,
454+ to_name = target_name ,
455+ relation_type = rel .type ,
456+ context = rel .context ,
451457 )
452- continue
458+ try :
459+ await self .relation_repository .add (relation )
460+ except IntegrityError :
461+ # Unique constraint violation - relation already exists
462+ logger .debug (
463+ f"Skipping duplicate relation { rel .type } from { db_entity .permalink } target: { rel .target } "
464+ )
465+ continue
453466
454467 return await self .repository .get_by_file_path (path )
455468
0 commit comments