3.8 KiB
File Access Fix - Database Type Mismatch
Problem
Users were uploading files successfully, but when the AI tried to execute code using load_file(), it would get the error:
ValueError: File 'xxx' not found or not accessible.
No files are currently accessible. Make sure to upload a file first.
Root Cause
Data Type Mismatch in Database Query
The issue was in src/database/db_handler.py in the get_user_files() method:
What Was Happening:
-
File Upload (
code_interpreter.py):expires_at = (datetime.now() + timedelta(hours=48)).isoformat() # Result: "2025-10-04T22:26:25.044108" (ISO string) -
Database Query (
db_handler.py):current_time = datetime.now() # datetime object files = await self.db.user_files.find({ "user_id": user_id, "$or": [ {"expires_at": {"$gt": current_time}}, # Comparing string > datetime ❌ {"expires_at": None} ] }).to_list(length=1000) -
Result: MongoDB couldn't compare ISO string with datetime object, so the query returned 0 files.
Logs Showing the Issue:
2025-10-02 22:26:25,106 - [DEBUG] Saved file metadata to database: 878573881449906208_1759418785_112e8587
2025-10-02 22:26:34,964 - [DEBUG] Fetched 0 files from DB for user 878573881449906208 ❌
2025-10-02 22:26:34,964 - [DEBUG] No files found in database for user 878573881449906208 ❌
Solution
Changed database query to use ISO string format for time comparison:
# Before:
current_time = datetime.now() # datetime object
# After:
current_time = datetime.now().isoformat() # ISO string
This ensures both values are ISO strings, making the MongoDB comparison work correctly.
Files Modified
-
src/database/db_handler.py(Line 344)- Changed
current_time = datetime.now()tocurrent_time = datetime.now().isoformat() - Added debug logging to show query results
- Changed
-
src/module/message_handler.py(Lines 327-339)- Added comprehensive debug logging to trace file fetching
-
src/utils/code_interpreter.py(Lines 153-160)- Changed
insert_onetoupdate_onewithupsert=Trueto avoid duplicate key errors - Added debug logging for database saves
- Changed
-
src/module/message_handler.py(Lines 637-680, 716-720)- Updated data analysis feature to use
load_file()with file IDs - Added
user_filesparameter toexecute_code()call
- Updated data analysis feature to use
Testing
After the fix, the flow should work correctly:
-
Upload File:
✅ Saved file metadata to database: 878573881449906208_1759418785_112e8587 -
Fetch Files:
✅ [DEBUG] Query returned 1 files for user 878573881449906208 ✅ Code execution will have access to 1 file(s) for user 878573881449906208 -
Execute Code:
✅ Processing 1 file(s) for code execution ✅ Added file to execution context: 878573881449906208_1759418785_112e8587 -> /path/to/file ✅ Total files accessible in execution: 1 -
Load File in Code:
df = pd.read_excel(load_file('878573881449906208_1759418785_112e8587')) # ✅ Works!
Restart Required
Yes, restart the bot to apply the changes:
# Stop the bot (Ctrl+C)
# Then restart:
python3 bot.py
Prevention
To prevent similar issues in the future:
- Consistent date handling: Always use the same format (ISO strings or datetime objects) throughout the codebase
- Add debug logging: Log database queries and results to catch data type mismatches
- Test file access: After any database schema changes, test the full file upload → execution flow
Related Issues
- File upload was working ✅
- Database saving was working ✅
- Database query was failing due to type mismatch ❌
- Code execution couldn't find files ❌
All issues now resolved! ✅