{"record":{"id":"07cdcea252b26746","repo":"invoke-ai/InvokeAI","slug":"cannot-relate-a-model-to-itself-07cdce","errorCode":null,"errorMessage":"Cannot relate a model to itself.","messagePattern":"Cannot relate a model to itself\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"warning","filePath":"invokeai/app/services/model_relationship_records/model_relationship_records_sqlite.py","lineNumber":15,"sourceCode":"from invokeai.app.services.model_relationship_records.model_relationship_records_base import (\n    ModelRelationshipRecordStorageBase,\n)\nfrom invokeai.app.services.shared.sqlite.sqlite_database import SqliteDatabase\n\n\nclass SqliteModelRelationshipRecordStorage(ModelRelationshipRecordStorageBase):\n    def __init__(self, db: SqliteDatabase) -> None:\n        super().__init__()\n        self._db = db\n\n    def add_model_relationship(self, model_key_1: str, model_key_2: str) -> None:\n        with self._db.transaction() as cursor:\n            if model_key_1 == model_key_2:\n                raise ValueError(\"Cannot relate a model to itself.\")\n            a, b = sorted([model_key_1, model_key_2])\n            cursor.execute(\n                \"INSERT OR IGNORE INTO model_relationships (model_key_1, model_key_2) VALUES (?, ?)\",\n                (a, b),\n            )\n\n    def remove_model_relationship(self, model_key_1: str, model_key_2: str) -> None:\n        with self._db.transaction() as cursor:\n            a, b = sorted([model_key_1, model_key_2])\n            cursor.execute(\n                \"DELETE FROM model_relationships WHERE model_key_1 = ? AND model_key_2 = ?\",\n                (a, b),\n            )\n\n    def get_related_model_keys(self, model_key: str) -> list[str]:\n        with self._db.transaction() as cursor:\n            cursor.execute(\n                \"\"\"","sourceCodeStart":1,"sourceCodeEnd":33,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/services/model_relationship_records/model_relationship_records_sqlite.py#L1-L33","documentation":"add_model_relationship(model_key_1, model_key_2) stores a symmetric relationship between two distinct models (keys are sorted and inserted with OR IGNORE). Relating a model to itself is meaningless, so it raises ValueError('Cannot relate a model to itself.') before touching the database.","triggerScenarios":"Calling add_model_relationship(key, key) with the same key for both arguments — often from code that builds relationship pairs from a list without excluding self-pairs, or from a UI bug passing the same selected model twice.","commonSituations":"Deduplicating relationship lists where (a,b) collapses to the same key; wiring up batch 'relate all in this folder' logic that iterates including the model itself; pasting the same key into both form fields.","solutions":["Guard the call site: only invoke when model_key_1 != model_key_2.","Wrap in try/except ValueError and skip/log self-relationships.","When generating pairs, use itertools.combinations(keys, 2) so self-pairs never occur.","Validate the two keys differ in any UI/API layer before calling the record service."],"exampleFix":"// before\nrecords.add_model_relationship(key, key)\n// after\nif key != other_key:\n    records.add_model_relationship(key, other_key)","handlingStrategy":"validation","validationCode":"if model_key_1 != model_key_2:\n    records.add_model_relationship(model_key_1, model_key_2)","typeGuard":"def is_valid_pair(a: str, b: str) -> bool:\n    return bool(a) and bool(b) and a != b","tryCatchPattern":"try:\n    records.add_model_relationship(k1, k2)\nexcept ValueError:\n    logger.debug(\"skipped self-relationship for %s\", k1)","preventionTips":["Generate pairs with itertools.combinations(keys, 2) to exclude self-pairs","Validate distinct keys in UI forms before submitting","Deduplicate (a,b) pairs via sorted-tuple sets before inserting","Treat self-relationships as no-ops in batch relate operations"],"tags":["invokeai","model-relationships","valueerror","argument-mismatch"],"backgroundTag":"self-reference-forbidden","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}