Fix doc ID mismatch in KB document upload

Create DB record first to get the auto-generated UUID, then use that ID
for the storage key. Previously a separate UUID was generated for storage
but the DB record got a different one, causing file retrieval to fail
during processing.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
michael 2026-02-12 15:50:35 -06:00
parent 1601622e07
commit 0b7213af01

View file

@ -162,29 +162,29 @@ async def upload_source_document(
# Read file data
file_data = await file.read()
doc_id = uuid.uuid4()
# Store file
storage_key = await storage_service.store_kb_document(
file_data=file_data,
kb_id=kb_id,
doc_id=doc_id,
filename=file.filename or "unknown",
mime_type=file.content_type or "application/octet-stream",
)
# Create DB record
# Create DB record first to get the ID
doc = await repo.add_source_document(
knowledge_base_id=kb_id,
filename=file.filename or "unknown",
file_storage_key=storage_key,
file_storage_key="pending", # Will update after storing
file_size_bytes=len(file_data),
mime_type=file.content_type or "application/octet-stream",
uploaded_by_id=user_id,
uploaded_by_name=user_name,
)
# Set the pre-generated ID
doc.id = doc_id
# Store file using the DB-generated doc ID
storage_key = await storage_service.store_kb_document(
file_data=file_data,
kb_id=kb_id,
doc_id=doc.id,
filename=file.filename or "unknown",
mime_type=file.content_type or "application/octet-stream",
)
# Update the storage key
doc.file_storage_key = storage_key
return SourceDocumentResponse(
id=doc.id,