Skip to content

Commit d5ac5c6

Browse files
claude[bot]groksrc
andauthored
fix: scope entity queries by project_id in upsert_entity method
Replace direct select(Entity) calls with self.select() to ensure proper project filtering in the upsert_entity method. This prevents "multiple rows" errors when similar permalinks exist across different projects. Fixes issue where: - File sync fails with "Multiple rows were found when one or none was required" - Similar permalinks across projects (e.g., "foo" and "foo-complete") cause conflicts - Queries were not properly scoped by project_id constraint Changed in EntityRepository.upsert_entity(): - Query after entity update (lines 144-148) - Query after entity insert (lines 164-168) - Query after race condition update (lines 205-210) - Query after permalink conflict resolution (lines 245-249) Co-authored-by: Drew Cain <groksrc@users.noreply.github.com>
1 parent a52ce1c commit d5ac5c6

1 file changed

Lines changed: 4 additions & 4 deletions

File tree

src/basic_memory/repository/entity_repository.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ async def upsert_entity(self, entity: Entity) -> Entity:
142142
await session.flush()
143143
# Return with relationships loaded
144144
query = (
145-
select(Entity)
145+
self.select()
146146
.where(Entity.file_path == entity.file_path)
147147
.options(*self.get_load_options())
148148
)
@@ -162,7 +162,7 @@ async def upsert_entity(self, entity: Entity) -> Entity:
162162

163163
# Return with relationships loaded
164164
query = (
165-
select(Entity)
165+
self.select()
166166
.where(Entity.file_path == entity.file_path)
167167
.options(*self.get_load_options())
168168
)
@@ -203,7 +203,7 @@ async def upsert_entity(self, entity: Entity) -> Entity:
203203
await session.flush()
204204
# Return the updated entity with relationships loaded
205205
query = (
206-
select(Entity)
206+
self.select()
207207
.where(Entity.file_path == entity.file_path)
208208
.options(*self.get_load_options())
209209
)
@@ -243,7 +243,7 @@ async def _handle_permalink_conflict(self, entity: Entity, session: AsyncSession
243243

244244
# Return the inserted entity with relationships loaded
245245
query = (
246-
select(Entity)
246+
self.select()
247247
.where(Entity.file_path == entity.file_path)
248248
.options(*self.get_load_options())
249249
)

0 commit comments

Comments
 (0)