{"record":{"id":"761ae4f011bed09b","repo":"infiniflow/ragflow","slug":"instance-model-obj-instance-id-not-found-for-mod","errorCode":null,"errorMessage":"Instance {model_obj.instance_id} not found for model {model_name}.","messagePattern":"Instance (.+?) not found for model (.+?)\\.","errorType":"exception","errorClass":"LookupError","httpStatus":null,"severity":"error","filePath":"api/db/joint_services/tenant_model_service.py","lineNumber":459,"sourceCode":"        # Verify tenant ownership through the provider chain\n        ok, provider_obj = TenantModelProviderService.get_by_id(model_obj.provider_id)\n        if not ok:\n            raise LookupError(f\"Provider id={model_obj.provider_id} not found for model {model_name}.\")\n        if tenant_id != provider_obj.tenant_id:\n            joined_tenants = TenantService.get_joined_tenants_by_user_id(tenant_id)\n            joined_tenant_ids = [t[\"tenant_id\"] for t in joined_tenants]\n            if provider_obj.tenant_id not in joined_tenant_ids:\n                raise LookupError(f\"Tenant {tenant_id} has no access to provider owned by tenant {provider_obj.tenant_id}.\")\n\n        exist_inst, instance_obj = TenantModelInstanceService.get_by_id(model_obj.instance_id)\n        if not exist_inst:\n            logger.warning(\n                \"Direct-ID resolution: instance not found | tenant_id=%s model_id=%s instance_id=%s\",\n                tenant_id,\n                model_name,\n                model_obj.instance_id,\n            )\n            raise LookupError(f\"Instance {model_obj.instance_id} not found for model {model_name}.\")\n        logger.debug(\n            \"Direct-ID resolution: resolved | tenant_id=%s model_id=%s instance_id=%s\",\n            tenant_id,\n            model_name,\n            model_obj.instance_id,\n        )\n        return instance_obj.api_key\n\n    # Fall back to name-based resolution: model[@instance]@provider\n    _, instance_name, provider_name = split_model_name(model_name)\n\n    if not provider_name:\n        raise LookupError(\"Provider name is required.\")\n    provider_obj = TenantModelProviderService.get_by_tenant_id_and_provider_name(tenant_id, provider_name)\n    if not provider_obj:\n        raise LookupError(f\"Provider {provider_name} not found.\")\n    instance_obj = _resolve_instance_for_model(provider_obj, instance_name, model_name)\n    return instance_obj.api_key","sourceCodeStart":441,"sourceCodeEnd":477,"githubUrl":"https://github.com/infiniflow/ragflow/blob/554fb1133ac3861732235ad9c377eb5e0a770665/api/db/joint_services/tenant_model_service.py#L441-L477","documentation":"LookupError raised by get_api_key (direct-ID path) when TenantModelInstanceService.get_by_id(model_obj.instance_id) fails: the tenant_model row points at a missing tenant_model_instance row. The API key actually lives on the instance row, so an orphaned instance reference makes key retrieval impossible. A warning is logged with tenant_id, model_id, instance_id before the raise.","triggerScenarios":"Calling get_api_key with a model id whose tenant_model.instance_id references a deleted instance row — e.g. an instance was removed during provider reconfiguration but the model row kept the stale FK, or partial deletion of a provider's instances.","commonSituations":"Editing a provider's API endpoints/instances and deleting the old instance while models still pointed at it; interrupted provider-edit transactions; DB restores with table inconsistency.","solutions":["Re-add or re-save the provider's instance (Model Providers -> edit) so instances and models are regenerated consistently, then re-select the model.","Delete the stale tenant_model row and re-register the model under a live instance.","Audit tenant_model rows whose instance_id has no tenant_model_instance counterpart and clean them.","Ensure provider-instance deletion cascades to or repoints dependent models."],"exampleFix":"-- find models pointing at missing instances\nSELECT m.id, m.instance_id FROM tenant_model m\nLEFT JOIN tenant_model_instance i ON i.id = m.instance_id\nWHERE i.id IS NULL;","handlingStrategy":"try-catch","validationCode":"exist, m = TenantModelService.get_by_id(model_id)\nif exist:\n    ok_inst, _ = TenantModelInstanceService.get_by_id(m.instance_id)\n    if not ok_inst:\n        raise ValueError(\"Model references a deleted instance; re-save the provider\")","typeGuard":null,"tryCatchPattern":"try:\n    key = get_api_key(tenant_id, model_id)\nexcept LookupError as e:\n    if \"Instance\" in str(e) and \"not found\" in str(e):\n        # stale FK: re-save provider config to regenerate instances\n        ...","preventionTips":["When editing provider instances, repoint or remove dependent tenant_model rows in the same transaction.","Watch for the logged warning 'Direct-ID resolution: instance not found' — it names the stale instance_id.","Scheduled integrity joins between tenant_model and tenant_model_instance catch drift early."],"tags":["referential-integrity","instance","api-key","ragflow"],"backgroundTag":null,"analyzedSha":"554fb1133ac3861732235ad9c377eb5e0a770665","analyzedAt":"2026-08-15T09:20:16.380Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}