{"record":{"id":"a1e8b55b4553e698","repo":"invoke-ai/InvokeAI","slug":"cannot-unlink-a-model-from-itself","errorCode":null,"errorMessage":"Cannot unlink a model from itself.","messagePattern":"Cannot unlink a model from itself\\.","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"warning","filePath":"invokeai/app/api/routers/model_relationships.py","lineNumber":159,"sourceCode":"        404: {\"description\": \"The relationship does not exist\"},\n        422: {\"description\": \"Validation error\"},\n        500: {\"description\": \"Internal server error\"},\n    },\n    summary=\"Remove Model Relationship\",\n    description=\"Removes a **bidirectional** relationship between two models. The relationship must already exist.\",\n)\ndef remove_model_relationship(\n    current_user: AdminUserOrDefault,\n    req: ModelRelationshipCreateRequest = Body(..., description=\"The model keys to disconnect\"),\n) -> None:\n    \"\"\"\n    Removes a bidirectional relationship between two model keys.\n\n    - Raises 400 if attempting to unlink a model from itself.\n    - Raises 404 if the relationship was not found.\n    \"\"\"\n    if req.model_key_1 == req.model_key_2:\n        raise HTTPException(status_code=400, detail=\"Cannot unlink a model from itself.\")\n\n    try:\n        ApiDependencies.invoker.services.model_relationships.remove_model_relationship(\n            req.model_key_1,\n            req.model_key_2,\n        )\n    except ValueError as e:\n        raise HTTPException(status_code=404, detail=str(e))\n\n\n@model_relationships_router.post(\n    \"/batch\",\n    operation_id=\"get_related_models_batch\",\n    response_model=List[str],\n    responses={\n        200: {\n            \"description\": \"Related model keys retrieved successfully\",\n            \"content\": {","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/api/routers/model_relationships.py#L141-L177","documentation":"HTTP 400 raised by DELETE /models/relationships when model_key_1 equals model_key_2 in the request body. A model cannot be unlinked from itself; the endpoint validates this before touching the service layer.","triggerScenarios":"DELETE /api/v1/models/relationships with identical keys in both fields of the request body — usually the same client bug that would have produced the self-relate 400 at add time.","commonSituations":"Symmetric cleanup scripts calling delete for every pair including degenerate pairs; stale client state passing one key twice.","solutions":["Guard that model_key_1 !== model_key_2 before issuing the DELETE","Filter out degenerate pairs when generating unlink requests from stored relationships","Fix the UI/form sending one key in both fields"],"exampleFix":"// before\nawait api.removeRelationship({ model_key_1: k, model_key_2: k });\n// after\nif (k1 !== k2) await api.removeRelationship({ model_key_1: k1, model_key_2: k2 });","handlingStrategy":"validation","validationCode":"if (req.model_key_1 === req.model_key_2) {\n  throw new Error('Cannot unlink a model from itself');\n}","typeGuard":null,"tryCatchPattern":"try {\n  await api.removeRelationship(req);\n} catch (e) {\n  if (e.status === 400 && e.body?.detail === 'Cannot unlink a model from itself.') throw new Error('self-pair in payload');\n  throw e;\n}","preventionTips":["Filter degenerate (k,k) pairs out of unlink batches","Derive unlink pairs from stored relationships, never from a single key","Mirror the add-side self-pair guard in delete paths"],"tags":["http-400","validation","model-relationships"],"backgroundTag":"self-referential-relationship","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}