Repository navigation
fix(db): drop Peewee-auto-named unique index on tenant_model_instance (#15699) - #15879
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
📝 WalkthroughWalkthroughThis PR updates the database migration to remove an additional legacy compound unique index that causes duplicate key constraint violations when creating new provider instances. The migration now drops the auto-named ChangesDatabase Migration Cleanup
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~3 minutes Poem
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #15879 +/- ##
=======================================
Coverage 93.16% 93.16%
=======================================
Files 10 10
Lines 717 717
Branches 118 118
=======================================
Hits 668 668
Misses 29 29
Partials 20 20 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Summary
Fixes #15699.
User upgrades to v0.25.6 against an existing MySQL database, tries to add an Ollama provider instance, and gets:
The route at api/apps/restful_apis/provider_api.py:354 catches it and returns
get_error_data_result(message="Internal server error")— which by RAGFlow's convention is HTTP 200 with an errorcodeon the body — hence the reporter's "200 status code but the database errored" complaint.Root cause
The provider-instance refactor in PR #15460 dropped the unique-compound-index tuple from
TenantModelInstance:and added a one-shot drop in
migrate_db()for existing databases. But the drop targets the wrong index name:Peewee's auto-derived index name is
<lowercase classname>_<col1>_<col2>→tenantmodelinstance_api_key_provider_id, which matches the user's error verbatim. The drop raisesOperationalError: 1091 (HY000): Can't DROP …, the surroundingexceptclause at db_models.py:1736 swallows it as expected-on-fresh-installs, and the legacy unique index lives on indefinitely.Why Ollama hits it specifically
Ollama doesn't require an API key. The form posts
api_key: "". The app-layer dedupe at provider_api_service.py:288-292:falls through for empty keys. Control reaches
TenantModelInstanceService.create_instance(..., api_key="")which inserts a row whose(api_key, provider_id) = ("", <provider_uuid>)collides with any prior Ollama row that already shipped that same pair → the still-present unique index throws.(
dbaafbfe608a11f1a5516d6066988224in the user's error is the duplicatedprovider_idUUID, paired with the emptyapi_key.)Fix
Add the Peewee auto-name alongside the existing
idx_*entry so the migration finally drops the obsolete index on next restart:The surrounding
try/except (OperationalError, ProgrammingError)matches1091/can't DROP/does not existand treats them as success, so every state is idempotent (see Test plan).Idempotency matrix
idx_api_key_provider_id)tenantmodelinstance_api_key_provider_id)1091→ swallowed1091→ swallowed1091→ swallowedidx_*1091→ swallowedmigrate_db()after either of the above1091→ swallowed1091→ swallowedNo rollback hazard: nothing depends on this unique constraint anymore (
create_instancededupes byinstance_nameviaduplicate_name, see tenant_model_instance_service.py:27).What this PR does NOT change
provider_api_service.create_provider_instance— itsif api_key:gate is correct for the post-migration world: multiple Ollama instances with empty keys under one provider are legitimate, so we shouldn't tighten the app-layer check.TenantModelInstancePeewee model — theindexestuple was already removed in Fix: model-provider bugs #15460. New databases never get the constraint in the first place.except → get_error_data_result→ HTTP 200 pattern atprovider_api.py:354— that's a project-wide convention; changing one route to HTTP 500 would be inconsistent and out of scope.Test plan
api_key, then try to create a second instance under the same provider — confirm theDuplicate entry … 'tenantmodelinstance_api_key_provider_id'error in the server log.SHOW INDEX FROM tenant_model_instance WHERE Key_name = 'tenantmodelinstance_api_key_provider_id';— non-empty result.migrate_db()runs, noFailed to drop indexin critical logs.SHOW INDEXquery — empty result.api_key="", both succeed.1091swallow keeps the migration idempotent.1091noise (the new index never existed), no functional regression.Files changed
tenantmodelinstance_api_key_provider_id; refactor the inline list to a namedlegacy_indexeslocal with a comment pointing at Fix: model-provider bugs #15460 and [Bug]: he ragfow-v0.25.6 version encounters errors when configuring the local LLM and embedding models pulled by ollama #15699.Type of change