mirror of
https://github.com/basicmachines-co/basic-memory
synced 2026-06-21 13:47:35 +00:00
Signed-off-by: phernandez <paul@basicmachines.co> Co-authored-by: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
from pathlib import Path
|
||||
from typing import List, Optional, Sequence, Union
|
||||
|
||||
from loguru import logger
|
||||
from sqlalchemy import select
|
||||
from sqlalchemy.exc import IntegrityError
|
||||
from sqlalchemy.ext.asyncio import AsyncSession, async_sessionmaker
|
||||
@@ -163,25 +164,32 @@ class EntityRepository(Repository[Entity]):
|
||||
|
||||
if existing_entity:
|
||||
# File path conflict - update the existing entity
|
||||
for key, value in {
|
||||
"title": entity.title,
|
||||
"entity_type": entity.entity_type,
|
||||
"entity_metadata": entity.entity_metadata,
|
||||
"content_type": entity.content_type,
|
||||
"permalink": entity.permalink,
|
||||
"checksum": entity.checksum,
|
||||
"updated_at": entity.updated_at,
|
||||
}.items():
|
||||
setattr(existing_entity, key, value)
|
||||
logger.debug(
|
||||
f"Resolving file_path conflict for {entity.file_path}, "
|
||||
f"entity_id={existing_entity.id}, observations={len(entity.observations)}"
|
||||
)
|
||||
# Use merge to avoid session state conflicts
|
||||
# Set the ID to update existing entity
|
||||
entity.id = existing_entity.id
|
||||
|
||||
# Clear and re-add observations
|
||||
existing_entity.observations.clear()
|
||||
# Ensure observations reference the correct entity_id
|
||||
for obs in entity.observations:
|
||||
obs.entity_id = existing_entity.id
|
||||
existing_entity.observations.append(obs)
|
||||
# Clear any existing ID to force INSERT as new observation
|
||||
obs.id = None
|
||||
|
||||
# Merge the entity which will update the existing one
|
||||
merged_entity = await session.merge(entity)
|
||||
|
||||
await session.commit()
|
||||
return existing_entity
|
||||
|
||||
# Re-query to get proper relationships loaded
|
||||
final_result = await session.execute(
|
||||
select(Entity)
|
||||
.where(Entity.id == merged_entity.id)
|
||||
.options(*self.get_load_options())
|
||||
)
|
||||
return final_result.scalar_one()
|
||||
|
||||
else:
|
||||
# No file_path conflict - must be permalink conflict
|
||||
|
||||
Reference in New Issue
Block a user