4.4 KiB
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:
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 infoget_custom_command_by_id(command_id: int)- Full command with all creator fieldscreate_custom_command(command_data: dict)- Create new commandupdate_custom_command(command_id: int, update_data: dict)- Update commanddelete_custom_command(command_id: int)- Delete commandget_creator_by_discord_id(discord_id: int)- Lookup creatorcreate_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
-
app/db_engine.py
- Added
Meta.table_name = 'custom_command_creators'toCustomCommandCreator - Added
Meta.table_name = 'custom_commands'toCustomCommand
- Added
-
app/routers_v3/custom_commands.py
- Added 7 helper functions using Peewee ORM (lines 95-185)
- Removed
DATABASE_TYPEchecks (lines 206, 951-956) - Simplified
execute_custom_command()to use helper functions (lines 894-922) - Added
creator_idto return value ofget_custom_command_by_name()(line 109)
-
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 paginationGET /api/v3/custom_commands/{command_id}- Get command by IDGET /api/v3/custom_commands/by_name/{command_name}- Get command by nameGET /api/v3/custom_commands/autocomplete- Autocomplete for DiscordGET /api/v3/custom_commands/stats- Command statisticsGET /api/v3/custom_commands/creators- List creators
Private Endpoints (require API token):
POST /api/v3/custom_commands- Create new commandPUT /api/v3/custom_commands/{command_id}- Update commandPATCH /api/v3/custom_commands/{command_id}- Partially update commandPATCH /api/v3/custom_commands/by_name/{name}/execute- Execute and track usageDELETE /api/v3/custom_commands/{command_id}- Delete commandPOST /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
- Always specify table names in Peewee models - Peewee's auto-generated names don't match PostgreSQL naming conventions
- Use Peewee ORM over raw SQL - Better type safety, maintainability, and less error-prone
- PostgreSQL migration complete - No need for DATABASE_TYPE conditionals anymore
- Helper functions should return all necessary fields - Including foreign key IDs, not just related object data