{"record":{"id":"badb015aa17717e8","repo":"invoke-ai/InvokeAI","slug":"cannot-relate-a-model-to-itself","errorCode":null,"errorMessage":"Cannot relate a model to itself.","messagePattern":"Cannot relate a model to itself\\.","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"warning","filePath":"invokeai/app/api/routers/model_relationships.py","lineNumber":124,"sourceCode":"        500: {\"description\": \"Internal server error\"},\n    },\n    summary=\"Add Model Relationship\",\n    description=\"Creates a **bidirectional** relationship between two models, allowing each to reference the other as related.\",\n)\ndef add_model_relationship(\n    current_user: AdminUserOrDefault,\n    req: ModelRelationshipCreateRequest = Body(..., description=\"The model keys to relate\"),\n) -> None:\n    \"\"\"\n    Add a relationship between two models.\n\n    Relationships are bidirectional and will be accessible from both models.\n\n    - Raises 400 if keys are invalid or identical.\n    - Raises 409 if the relationship already exists.\n    \"\"\"\n    if req.model_key_1 == req.model_key_2:\n        raise HTTPException(status_code=400, detail=\"Cannot relate a model to itself.\")\n\n    try:\n        ApiDependencies.invoker.services.model_relationships.add_model_relationship(\n            req.model_key_1,\n            req.model_key_2,\n        )\n    except ValueError as e:\n        raise HTTPException(status_code=409, detail=str(e))\n\n\n@model_relationships_router.delete(\n    \"/\",\n    status_code=status.HTTP_204_NO_CONTENT,\n    responses={\n        204: {\"description\": \"The relationship was successfully removed\"},\n        400: {\"description\": \"Invalid model keys or self-referential relationship\"},\n        404: {\"description\": \"The relationship does not exist\"},\n        422: {\"description\": \"Validation error\"},","sourceCodeStart":106,"sourceCodeEnd":142,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/api/routers/model_relationships.py#L106-L142","documentation":"HTTP 400 raised by POST /models/relationships when model_key_1 and model_key_2 in the request body are identical. Relationships are strictly between two distinct models; relating a model to itself is meaningless and rejected before any service call.","triggerScenarios":"Sending the same key in both fields of the AddModelRelationshipsRequest body to POST /api/v1/models/relationships, typically from a client that builds the pair from a single selected model.","commonSituations":"UI drag-and-drop onto itself; scripting relationship creation from a list where duplicates weren't deduplicated; copy-paste of the same key into both form fields.","solutions":["Check that model_key_1 !== model_key_2 in the request payload before sending","Skip or filter self-pairs when iterating model lists to create relationships","Correct the client code that is populating both keys with the same value"],"exampleFix":"// before\nawait api.addRelationship({ model_key_1: k, model_key_2: k });\n// after\nif (k1 !== k2) await api.addRelationship({ 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 relate a model to itself');\n}","typeGuard":null,"tryCatchPattern":"try {\n  await api.addRelationship(req);\n} catch (e) {\n  if (e.status === 400 && e.body?.detail === 'Cannot relate a model to itself.') throw new Error('self-pair in payload');\n  throw e;\n}","preventionTips":["Always build relationship payloads from two distinct selected models","Deduplicate key pairs before batch operations","Make the UI prevent dropping a model onto itself"],"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"}