fix(templates): Database error detected when saving a template with a disk image change

This commit is contained in:
Cristi 2026-06-06 14:21:15 +03:00
parent 13d1563762
commit da3a34144b
3 changed files with 9 additions and 3 deletions

View File

@ -48,7 +48,7 @@ class ImagesRepository(BaseRepository):
else:
query = select(models.Image).where(models.Image.filename == image_name)
result = await self._db_session.execute(query)
return result.scalars().one_or_none()
return result.scalars().first()
async def get_image_by_checksum(self, checksum: str, image_dir: str = None) -> Optional[models.Image]:
"""

View File

@ -124,7 +124,9 @@ class TemplatesRepository(BaseRepository):
else:
query = select(models.Image).where(models.Image.filename == image_name)
result = await self._db_session.execute(query)
return result.scalars().one_or_none()
# Use first() instead of one_or_none() to handle cases where multiple
# DB rows share the same filename (e.g. image discovered in multiple paths)
return result.scalars().first()
async def add_image_to_template(
self,

View File

@ -267,9 +267,13 @@ class TemplatesService:
raise ControllerNotFoundError(f"Template '{template_id}' not found")
return template
async def _remove_image(self, template_id: UUID, image_path:str) -> None:
async def _remove_image(self, template_id: UUID, image_path: str) -> None:
if not image_path:
return
image = await self._templates_repo.get_image(image_path)
if image is None:
return
await self._templates_repo.remove_image_from_template(template_id, image)
async def update_template(self, template_id: UUID, template_update: schemas.TemplateUpdate) -> dict: