invoke-ai/InvokeAI · warning · ValueError

Cannot relate a model to itself.

Error message

Cannot relate a model to itself.

What it means

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.

Source

Thrown at invokeai/app/services/model_relationship_records/model_relationship_records_sqlite.py:15

from invokeai.app.services.model_relationship_records.model_relationship_records_base import (
    ModelRelationshipRecordStorageBase,
)
from invokeai.app.services.shared.sqlite.sqlite_database import SqliteDatabase


class SqliteModelRelationshipRecordStorage(ModelRelationshipRecordStorageBase):
    def __init__(self, db: SqliteDatabase) -> None:
        super().__init__()
        self._db = db

    def add_model_relationship(self, model_key_1: str, model_key_2: str) -> None:
        with self._db.transaction() as cursor:
            if model_key_1 == model_key_2:
                raise ValueError("Cannot relate a model to itself.")
            a, b = sorted([model_key_1, model_key_2])
            cursor.execute(
                "INSERT OR IGNORE INTO model_relationships (model_key_1, model_key_2) VALUES (?, ?)",
                (a, b),
            )

    def remove_model_relationship(self, model_key_1: str, model_key_2: str) -> None:
        with self._db.transaction() as cursor:
            a, b = sorted([model_key_1, model_key_2])
            cursor.execute(
                "DELETE FROM model_relationships WHERE model_key_1 = ? AND model_key_2 = ?",
                (a, b),
            )

    def get_related_model_keys(self, model_key: str) -> list[str]:
        with self._db.transaction() as cursor:
            cursor.execute(
                """

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Guard the call site: only invoke when model_key_1 != model_key_2.
  2. Wrap in try/except ValueError and skip/log self-relationships.
  3. When generating pairs, use itertools.combinations(keys, 2) so self-pairs never occur.
  4. Validate the two keys differ in any UI/API layer before calling the record service.

Example fix

// before
records.add_model_relationship(key, key)
// after
if key != other_key:
    records.add_model_relationship(key, other_key)
Defensive patterns

Strategy: validation

Validate before calling

if model_key_1 != model_key_2:
    records.add_model_relationship(model_key_1, model_key_2)

Type guard

def is_valid_pair(a: str, b: str) -> bool:
    return bool(a) and bool(b) and a != b

Try / catch

try:
    records.add_model_relationship(k1, k2)
except ValueError:
    logger.debug("skipped self-relationship for %s", k1)

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29). Data as JSON: /api/errors/07cdcea252b26746. Report an issue: GitHub.