# Critical Database Field Fixes Required ## Summary Found 62 field name inconsistencies across 6 Python files that need to be fixed to match the database schema. ## Most Critical Issues ### 1. **Sessions Table Field References** The sessions table has duplicate columns that are causing confusion: | Database Column | Code References (Wrong) | Files Affected | |----------------|------------------------|----------------| | `hardware_id` | `device_id` | api_routes.py, session_routes.py, export_routes.py | | `is_active` | `active` | api_routes.py, session_routes.py, export_routes.py, batch_routes.py | | `started_at` | `login_time` | session_routes.py, export_routes.py | | `last_heartbeat` | `last_activity` | session_routes.py, export_routes.py | | `ended_at` | `logout_time` | api_routes.py, session_routes.py, export_routes.py | | `started_at` | `start_time` | models.py | ### 2. **Device Registration Issues** - `device_registrations` table uses `device_id` column (line 324 in api_routes.py) - But sessions table uses `hardware_id` - This creates a mismatch when joining tables ## Immediate Action Required ### Option 1: Fix Code (Recommended) Update all Python files to use the correct column names from the database schema. ### Option 2: Add Compatibility Columns (Temporary) ```sql -- Add missing columns to sessions for backward compatibility ALTER TABLE sessions ADD COLUMN IF NOT EXISTS device_id VARCHAR(100); UPDATE sessions SET device_id = hardware_id WHERE device_id IS NULL; -- Update device_registrations to use hardware_id ALTER TABLE device_registrations RENAME COLUMN device_id TO hardware_id; ``` ## Files That Need Updates 1. **routes/session_routes.py** (15 occurrences) - Lines: 84, 134, 325 (device_id) - Lines: 85, 109, 112, 119, 124, 135, 150 (login_time) - Lines: 86, 119, 136, 202, 248 (logout_time) - Lines: 87, 137 (last_activity) - Lines: 88, 124, 138, 192, 202, 236, 248, 249, 328, 340, 361 (active) 2. **routes/api_routes.py** (12 occurrences) - Lines: 203, 214, 345, 861 (device_id) - Lines: 204, 344, 345, 453, 455, 457, 862 (active) - Line: 344 (logout_time) 3. **routes/export_routes.py** (11 occurrences) - Lines: 47, 72, 224, 244 (device_id) - Lines: 46, 71, 228, 233, 248 (active) - Lines: 225, 234, 245, 253, 254 (login_time) - Lines: 226, 246 (logout_time) - Lines: 227, 247 (last_activity) 4. **models.py** (2 occurrences) - Line: 167 (start_time) - Line: 177 (active - but this is just in error message) 5. **routes/batch_routes.py** (2 occurrences) - Lines: 212, 213 (active) 6. **routes/customer_routes.py** (1 occurrence) - Line: 392 (comment mentions correction already made) ## Testing After Fixes 1. Test all session-related functionality 2. Verify device registration/deregistration works 3. Check session history displays correctly 4. Ensure exports contain correct data 5. Validate batch operations still function