{"record":{"id":"5b4e1fa39257da88","repo":"infiniflow/ragflow","slug":"database-error-file-5b4e1f","errorCode":null,"errorMessage":"Database error (File)!","messagePattern":"Database error \\(File\\)!","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"api/db/services/file_service.py","lineNumber":466,"sourceCode":"            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:\n                parent_folders.append(file)\n                break\n        return parent_folders\n\n    @classmethod\n    @DB.connection_context()\n    def insert(cls, file):\n        # Insert a new file record\n        # Args:\n        #     file: File data dictionary\n        # Returns:\n        #     Created file object\n        if not cls.save(**file):\n            raise RuntimeError(\"Database error (File)!\")\n        return File(**file)\n\n    @classmethod\n    @DB.connection_context()\n    def delete(cls, file):\n        return cls.delete_by_id(file.id)\n\n    @classmethod\n    @DB.connection_context()\n    def delete_by_pf_id(cls, folder_id):\n        return cls.model.delete().where(cls.model.parent_id == folder_id).execute()\n\n    @classmethod\n    @DB.connection_context()\n    def delete_folder_by_pf_id(cls, user_id, folder_id):\n        try:\n            files = cls.model.select().where((cls.model.tenant_id == user_id) & (cls.model.parent_id == folder_id))\n            for file in files:","sourceCodeStart":448,"sourceCodeEnd":484,"githubUrl":"https://github.com/infiniflow/ragflow/blob/554fb1133ac3861732235ad9c377eb5e0a770665/api/db/services/file_service.py#L448-L484","documentation":"Raised by FileService.insert when cls.save(**file) returns falsy, meaning Peewee failed to persist the new File record (or save returned 0 rows affected). The RuntimeError signals the INSERT did not succeed, typically a constraint violation or invalid field data surfaced through save().","triggerScenarios":"Inserting a file dict with a duplicate primary key id, a parent_id that violates a foreign-key constraint, a tenant_id that does not exist, missing required columns, or a field value of the wrong type.","commonSituations":"Reusing a client-supplied uuid that already exists; uploading into a folder id that was deleted mid-request; schema drift after a RAGFlow upgrade adding NOT NULL columns; passing None for tenant_id.","solutions":["Inspect the server log for the underlying Peewee/MySQL error logged just before this raise; fix the identified column/constraint.","Ensure file['id'] is a fresh uuid (get_uuid()) and not an existing primary key.","Verify parent_id references an existing file row and tenant_id references an existing tenant.","After upgrading RAGFlow, re-run migrations so the file table schema matches the model."],"exampleFix":"# before\nfile['id'] = client_provided_id  # may collide\nFileService.insert(file)\n\n# after\nfile['id'] = get_uuid()\nFileService.insert(file)","handlingStrategy":"try-catch","validationCode":"required = {'id', 'parent_id', 'tenant_id', 'name', 'type'}\nmissing = required - set(file.keys())\nassert not missing, f'missing fields: {missing}'\nok, parent = FileService.get_by_id(file['parent_id'])\nassert ok, 'parent folder must exist'","typeGuard":"def is_insertable_file_dict(file: dict) -> bool:\n    return (\n        isinstance(file, dict)\n        and isinstance(file.get('id'), str)\n        and not FileService.model.select().where(FileService.model.id == file['id']).exists()\n        and FileService.model.select().where(FileService.model.id == file.get('parent_id')).exists()\n    )","tryCatchPattern":"try:\n    FileService.insert(file)\nexcept RuntimeError:\n    logger.exception('file insert failed')\n    raise ValidationError('could not create file record')","preventionTips":["Always generate ids with get_uuid(); never reuse client-supplied ids.","Validate parent folder and tenant existence before insert.","Run DB migrations after each RAGFlow upgrade before accepting writes."],"tags":["database","insert","file-service","constraint-violation"],"backgroundTag":null,"analyzedSha":"554fb1133ac3861732235ad9c377eb5e0a770665","analyzedAt":"2026-08-15T09:20:16.380Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}