{"record":{"id":"c24b01ec5ec043df","repo":"invoke-ai/InvokeAI","slug":"key-does-not-match-new-config-key","errorCode":null,"errorMessage":"key does not match new_config.key","messagePattern":"key does not match new_config\\.key","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"invokeai/app/services/model_records/model_records_sql.py","lineNumber":227,"sourceCode":"                json_serialized = record.model_dump_json()\n\n            cursor.execute(\n                \"\"\"--sql\n                UPDATE models\n                SET\n                    config=?\n                WHERE id=?;\n                \"\"\",\n                (json_serialized, key),\n            )\n            if cursor.rowcount == 0:\n                raise UnknownModelException(\"model not found\")\n\n        return self.get_model(key)\n\n    def replace_model(self, key: str, new_config: AnyModelConfig) -> AnyModelConfig:\n        if key != new_config.key:\n            raise ValueError(\"key does not match new_config.key\")\n        with self._db.transaction() as cursor:\n            cursor.execute(\n                \"\"\"--sql\n                UPDATE models\n                SET\n                    config=?\n                WHERE id=?;\n                \"\"\",\n                (new_config.model_dump_json(), key),\n            )\n            if cursor.rowcount == 0:\n                raise UnknownModelException(\"model not found\")\n        return self.get_model(key)\n\n    def get_model(self, key: str) -> AnyModelConfig:\n        \"\"\"\n        Retrieve the ModelConfigBase instance for the indicated model.\n","sourceCodeStart":209,"sourceCodeEnd":245,"githubUrl":"https://github.com/invoke-ai/InvokeAI/blob/0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06/invokeai/app/services/model_records/model_records_sql.py#L209-L245","documentation":"replace_model() overwrites an existing model record wholesale with new_config. To prevent accidentally re-keying a record, it requires that the `key` argument equals new_config.key; a mismatch means the caller is trying to replace one record with a config belonging to a different model, so ValueError is raised before any DB write.","triggerScenarios":"Calling replace_model(existing_key, new_config) where new_config.key differs from existing_key — e.g. constructing a modified copy of a config without copying its `.key`, or passing a config loaded for a different model.","commonSituations":"Copying a model config object and editing fields but not preserving `.key`; passing a freshly built config (key None/default) instead of a fetched one; mixing up configs when batch-editing many models.","solutions":["Set new_config.key = key (or build the new config from the fetched one so `.key` is preserved) before calling replace_model.","Fetch the current config with get_model(key), copy() it, modify fields, and pass the copy back — its key stays consistent.","If you actually want a new model entry, use add_model(new_config) instead of replace_model.","Add an assertion comparing key == new_config.key early in your editing code to catch drift."],"exampleFix":"// before\nnew_config = model_config.copy(update={\"name\": \"new-name\"})\nrecords.replace_model(other_key, new_config)\n// after\nnew_config = model_config.copy(update={\"name\": \"new-name\", \"key\": other_key})\nrecords.replace_model(other_key, new_config)","handlingStrategy":"validation","validationCode":"assert key == new_config.key, f\"replace_model: {key!r} != {new_config.key!r}\"\nrecords.replace_model(key, new_config)","typeGuard":"def is_consistent_replace(key: str, cfg) -> bool:\n    return getattr(cfg, \"key\", None) == key","tryCatchPattern":"try:\n    records.replace_model(key, new_config)\nexcept ValueError as e:\n    if \"key does not match\" in str(e):\n        new_config.key = key\n        records.replace_model(key, new_config)","preventionTips":["Build replacement configs via copy(update=...) from the fetched record so .key is preserved","Never hand-construct configs for replace_model; fetch-then-modify","Add an assertion comparing keys before calling","Use add_model when you actually want a new record"],"tags":["invokeai","model-manager","argument-mismatch","valueerror"],"backgroundTag":"key-mismatch","analyzedSha":"0b6a024f2ff6a86bfb953dcdb9cc504ef7397a06","analyzedAt":"2026-08-29T04:46:49.967Z","schemaVersion":2},"datasetVersion":"2026-08-29T07:17:48.351Z"}