SQLAlchemy was throwing "Table already defined" errors when trying to initialize the database with workspace, AI keys, and webhook models.
Tables were being redefined when models were imported multiple times without the extend_existing parameter.
workspace.py:
WorkspacemodelWorkspaceInvitationmodelWorkspaceActivitymodelWorkspaceWebhookmodelResourcePermissionmodel (special handling for constraints)
ai_keys.py:
AIKeymodelAIKeyUsagemodel
webhook.py:
- Fixed import from non-existent
.basetosrc.database WebhookmodelWebhookDeliverymodel
workspace_members table:
- Added
extend_existing=Trueto the association table definition
- Changed
WebhookEndpointtoWebhook(correct class name) - Removed
WebhookEventfrom imports (it's an enum, not a model)
/backend/src/models/workspace.py- Added__table_args__ = {'extend_existing': True}to all classes/backend/src/models/ai_keys.py- Added__table_args__ = {'extend_existing': True}to both classes/backend/src/models/webhook.py- Fixed import and added__table_args__/backend/src/database.py- Fixed model imports ininit_db()
✅ Database initialized successfully!
✅ Database initialization completed without errorsDatabase now initializes without any table redefinition errors. All models can be imported and tables created successfully.
The remaining database issue has been completely resolved. The system can now:
- Initialize all tables without errors
- Import all models without conflicts
- Create workspaces, AI keys, and webhooks properly
- Handle multiple imports gracefully with
extend_existing=True