# Custom Commands Fix Summary **Date**: 2025-10-19 **Issue**: Custom commands endpoint failing with "relation 'customcommand' does not exist" and "'creator_id' not defined" errors ## Problems Fixed ### 1. Peewee Table Name Mismatch **Problem**: Peewee was auto-generating table names as `customcommand` and `customcommandcreator` (lowercase, no underscores), but PostgreSQL tables were named `custom_commands` and `custom_command_creators`. **Fix**: Added `Meta` classes to both models in `app/db_engine.py`: ```python class CustomCommandCreator(BaseModel): # ... fields ... class Meta: table_name = 'custom_command_creators' class CustomCommand(BaseModel): # ... fields ... class Meta: table_name = 'custom_commands' ``` ### 2. Missing Helper Functions **Problem**: `app/routers_v3/custom_commands.py` was calling undefined functions like `get_custom_command_by_name()`, `create_custom_command()`, etc. **Fix**: Implemented all helper functions using Peewee ORM (lines 95-185): - `get_custom_command_by_name(name: str)` - Case-insensitive lookup with creator info - `get_custom_command_by_id(command_id: int)` - Full command with all creator fields - `create_custom_command(command_data: dict)` - Create new command - `update_custom_command(command_id: int, update_data: dict)` - Update command - `delete_custom_command(command_id: int)` - Delete command - `get_creator_by_discord_id(discord_id: int)` - Lookup creator - `create_creator(creator_data: dict)` - Create new creator ### 3. DATABASE_TYPE References **Problem**: Code referenced `DATABASE_TYPE` for conditional SQL (SQLite vs PostgreSQL), but variable wasn't imported. **Fix**: Removed all `DATABASE_TYPE` checks and use PostgreSQL-specific syntax (`ILIKE`) directly since migration is complete. ### 4. Missing creator_id in execute_custom_command **Problem**: `execute_custom_command()` was failing because `get_custom_command_by_name()` didn't return `creator_id` field. **Fix**: Updated `get_custom_command_by_name()` to explicitly include `creator_id` in returned dict (line 109). ## Files Modified 1. **app/db_engine.py** - Added `Meta.table_name = 'custom_command_creators'` to `CustomCommandCreator` - Added `Meta.table_name = 'custom_commands'` to `CustomCommand` 2. **app/routers_v3/custom_commands.py** - Added 7 helper functions using Peewee ORM (lines 95-185) - Removed `DATABASE_TYPE` checks (lines 206, 951-956) - Simplified `execute_custom_command()` to use helper functions (lines 894-922) - Added `creator_id` to return value of `get_custom_command_by_name()` (line 109) 3. **CLAUDE.md** - Updated Database Configuration section - Removed DATABASE_TYPE from environment variables - Added Custom Commands to Key Data Models - Updated Important Notes to reflect PostgreSQL migration completion ## Endpoints Now Working All custom commands endpoints are now functional: **Public Endpoints**: - `GET /api/v3/custom_commands` - List/search commands with pagination - `GET /api/v3/custom_commands/{command_id}` - Get command by ID - `GET /api/v3/custom_commands/by_name/{command_name}` - Get command by name - `GET /api/v3/custom_commands/autocomplete` - Autocomplete for Discord - `GET /api/v3/custom_commands/stats` - Command statistics - `GET /api/v3/custom_commands/creators` - List creators **Private Endpoints** (require API token): - `POST /api/v3/custom_commands` - Create new command - `PUT /api/v3/custom_commands/{command_id}` - Update command - `PATCH /api/v3/custom_commands/{command_id}` - Partially update command - `PATCH /api/v3/custom_commands/by_name/{name}/execute` - Execute and track usage - `DELETE /api/v3/custom_commands/{command_id}` - Delete command - `POST /api/v3/custom_commands/creators` - Create new creator ## Testing Results ✅ Successfully retrieved creator by Discord ID ✅ Command creation returns proper 409 conflict for existing commands ✅ Command execution successfully updates usage statistics ✅ All endpoints return properly formatted JSON responses ## Key Learnings 1. **Always specify table names in Peewee models** - Peewee's auto-generated names don't match PostgreSQL naming conventions 2. **Use Peewee ORM over raw SQL** - Better type safety, maintainability, and less error-prone 3. **PostgreSQL migration complete** - No need for DATABASE_TYPE conditionals anymore 4. **Helper functions should return all necessary fields** - Including foreign key IDs, not just related object data