{"record":{"id":"636ee88b5c7e4902","repo":"infiniflow/ragflow","slug":"database-error-file-doesn-t-exist","errorCode":null,"errorMessage":"Database error (File doesn't exist)!","messagePattern":"Database error \\(File doesn't exist\\)!","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"api/db/services/file_service.py","lineNumber":434,"sourceCode":"            kb_folder = cls.new_a_file_from_kb(tenant_id, kb.name, folder[\"id\"])\n            for doc in DocumentService.query(kb_id=kb.id):\n                FileService.add_file_from_kb(doc.to_dict(), kb_folder[\"id\"], tenant_id)\n\n    @classmethod\n    @DB.connection_context()\n    def get_parent_folder(cls, file_id):\n        # Get parent folder of a file\n        # Args:\n        #     file_id: File ID\n        # Returns:\n        #     Parent folder object\n        file = cls.model.select().where(cls.model.id == file_id)\n        if file.count():\n            e, file = cls.get_by_id(file[0].parent_id)\n            if not e:\n                raise RuntimeError(\"Database error (File retrieval)!\")\n        else:\n            raise RuntimeError(\"Database error (File doesn't exist)!\")\n        return file\n\n    @classmethod\n    @DB.connection_context()\n    def get_all_parent_folders(cls, start_id):\n        # Get all parent folders in path\n        # Args:\n        #     start_id: Starting file ID\n        # Returns:\n        #     List of parent folder objects\n        parent_folders = []\n        current_id = start_id\n        while current_id:\n            e, file = cls.get_by_id(current_id)\n            if e and file.parent_id != file.id:\n                parent_folders.append(file)\n                current_id = file.parent_id\n            else:","sourceCodeStart":416,"sourceCodeEnd":452,"githubUrl":"https://github.com/infiniflow/ragflow/blob/554fb1133ac3861732235ad9c377eb5e0a770665/api/db/services/file_service.py#L416-L452","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Verify the file exists first: e, f = FileService.get_by_id(file_id) and handle e == False gracefully before calling get_parent_folder.","Check the id type and value (must be a valid uuid string matching the file table's id column).","If the file was deleted, refresh or clear the stale reference in the caller/UI and stop the operation.","Inspect the file table (SELECT * FROM file WHERE id = ...) to confirm the row is present in the current database."],"exampleFix":"// before\nfolder = FileService.get_parent_folder(file_id)  # may raise RuntimeError\n\n// after\nexists, _ = FileService.get_by_id(file_id)\nif not exists:\n    return json_error_response(\"file not found\", code=404)\nfolder = FileService.get_parent_folder(file_id)","handlingStrategy":"validation","validationCode":"exists, file_row = FileService.get_by_id(file_id)\nif not exists:\n    return error_response(404, 'file not found')","typeGuard":"def is_valid_file_id(file_id: str) -> bool:\n    try:\n        uuid.UUID(str(file_id))\n    except (ValueError, TypeError):\n        return False\n    return FileService.model.select().where(FileService.model.id == file_id).exists()","tryCatchPattern":"try:\n    folder = FileService.get_parent_folder(file_id)\nexcept RuntimeError as e:\n    if \"doesn't exist\" in str(e):\n        return json_error_response('file not found', 404)\n    raise","preventionTips":["Always resolve file ids through a fresh API call before using cached references.","Handle 404 for deleted files in UI state management instead of retrying.","Never mix document ids and file ids; they are separate tables."],"tags":["database","not-found","file-service","runtime-error"],"backgroundTag":null,"analyzedSha":"554fb1133ac3861732235ad9c377eb5e0a770665","analyzedAt":"2026-08-15T09:20:16.380Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}