infiniflow/ragflow · error · RuntimeError

Database error (File doesn't exist)!

Error message

Database error (File doesn't exist)!

What it means

Raised by FileService.get_parent_folder when no File row matches the given file_id. The service first selects the file by id; if the query returns zero rows, it raises this RuntimeError. It is a not-found condition expressed as a database error, not an actual DBMS failure.

Source

Thrown at api/db/services/file_service.py:434

            kb_folder = cls.new_a_file_from_kb(tenant_id, kb.name, folder["id"])
            for doc in DocumentService.query(kb_id=kb.id):
                FileService.add_file_from_kb(doc.to_dict(), kb_folder["id"], tenant_id)

    @classmethod
    @DB.connection_context()
    def get_parent_folder(cls, file_id):
        # Get parent folder of a file
        # Args:
        #     file_id: File ID
        # Returns:
        #     Parent folder object
        file = cls.model.select().where(cls.model.id == file_id)
        if file.count():
            e, file = cls.get_by_id(file[0].parent_id)
            if not e:
                raise RuntimeError("Database error (File retrieval)!")
        else:
            raise RuntimeError("Database error (File doesn't exist)!")
        return file

    @classmethod
    @DB.connection_context()
    def get_all_parent_folders(cls, start_id):
        # Get all parent folders in path
        # Args:
        #     start_id: Starting file ID
        # Returns:
        #     List of parent folder objects
        parent_folders = []
        current_id = start_id
        while current_id:
            e, file = cls.get_by_id(current_id)
            if e and file.parent_id != file.id:
                parent_folders.append(file)
                current_id = file.parent_id
            else:

View on GitHub (pinned to 554fb1133a)

Solutions

  1. Verify the file exists first: e, f = FileService.get_by_id(file_id) and handle e == False gracefully before calling get_parent_folder.
  2. Check the id type and value (must be a valid uuid string matching the file table's id column).
  3. If the file was deleted, refresh or clear the stale reference in the caller/UI and stop the operation.
  4. Inspect the file table (SELECT * FROM file WHERE id = ...) to confirm the row is present in the current database.

Example fix

// before
folder = FileService.get_parent_folder(file_id)  # may raise RuntimeError

// after
exists, _ = FileService.get_by_id(file_id)
if not exists:
    return json_error_response("file not found", code=404)
folder = FileService.get_parent_folder(file_id)
Defensive patterns

Strategy: validation

Validate before calling

exists, file_row = FileService.get_by_id(file_id)
if not exists:
    return error_response(404, 'file not found')

Type guard

def is_valid_file_id(file_id: str) -> bool:
    try:
        uuid.UUID(str(file_id))
    except (ValueError, TypeError):
        return False
    return FileService.model.select().where(FileService.model.id == file_id).exists()

Try / catch

try:
    folder = FileService.get_parent_folder(file_id)
except RuntimeError as e:
    if "doesn't exist" in str(e):
        return json_error_response('file not found', 404)
    raise

Prevention

When it happens

Trigger: Calling get_parent_folder(file_id) with an id that does not exist in the file table: deleted file, id from another tenant/environment, stale id held in client state, or a malformed/typo'd uuid passed from an API route or SDK call.

Common situations: UI keeps a file id after the file was deleted elsewhere; concurrent deletion between listing a folder and requesting the parent; passing a document id where a file id is expected; migrating data without preserving file ids.

Related errors


AI-assisted analysis of infiniflow/ragflow@554fb1133a (2026-08-15). Data as JSON: /api/errors/636ee88b5c7e4902. Report an issue: GitHub.