tirth8205/code-review-graph · error · ValueError

Unknown table: {table}

Error message

Unknown table: {table}

What it means

_has_column() only accepts tables listed in the _KNOWN_TABLES whitelist before running PRAGMA table_info, raising ValueError for anything else. This guards migrations against typos and SQL injection via the interpolated table name.

Source

Thrown at code_review_graph/migrations.py:51

def _set_schema_version(conn: sqlite3.Connection, version: int) -> None:
    """Set the schema version in the metadata table."""
    conn.execute(
        "INSERT OR REPLACE INTO metadata (key, value) VALUES ('schema_version', ?)",
        (str(version),),
    )


_KNOWN_TABLES = frozenset({
    "nodes", "edges", "metadata", "communities", "flows", "flow_memberships", "nodes_fts",
    "community_summaries", "flow_snapshots", "risk_index",
})


def _has_column(conn: sqlite3.Connection, table: str, column: str) -> bool:
    """Check if a column exists in a table."""
    if table not in _KNOWN_TABLES:
        raise ValueError(f"Unknown table: {table}")
    cursor = conn.execute(f"PRAGMA table_info({table})")  # noqa: S608
    columns = [row[1] if isinstance(row, tuple) else row["name"] for row in cursor]
    return column in columns


def _table_exists(conn: sqlite3.Connection, table: str) -> bool:
    """Check if a table exists."""
    if table not in _KNOWN_TABLES:
        raise ValueError(f"Unknown table: {table}")
    row = conn.execute(
        "SELECT count(*) FROM sqlite_master WHERE type IN ('table', 'view') "
        "AND name = ?",
        (table,),
    ).fetchone()
    return row[0] > 0


# ---------------------------------------------------------------------------

View on GitHub (pinned to b58668751a)

Solutions

  1. Add the new table name to the _KNOWN_TABLES set in code_review_graph/migrations.py.
  2. Verify the spelling/case matches exactly (SQLite PRAGMA lookups are case-insensitive but the whitelist check is not).
  3. If hitting this as a library user on an old version after a schema change, upgrade the package so the whitelist matches the migrations.

Example fix

# before
_KNOWN_TABLES = {"nodes", "edges"}
_has_column(conn, "symbols", "kind")
# after
_KNOWN_TABLES = {"nodes", "edges", "symbols"}
_has_column(conn, "symbols", "kind")
Defensive patterns

Strategy: validation

Validate before calling

from code_review_graph.migrations import _KNOWN_TABLES
if table not in _KNOWN_TABLES:
    raise ValueError(f"register {table!r} in _KNOWN_TABLES before probing columns")

Prevention

When it happens

Trigger: A migration (e.g. _migrate_v2, _migrate_v4, _migrate_v9) calls _has_column with a table name that is not in _KNOWN_TABLES — typically after a schema change adds a new table without updating the whitelist, or an internal call passes a renamed table.

Common situations: Contributors adding new tables to the schema in a new migration but forgetting to register them in _KNOWN_TABLES; refactors renaming tables; version skew between migration code and the whitelist constant.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


AI-assisted analysis of tirth8205/code-review-graph@b58668751a (2026-08-28). Data as JSON: /api/errors/309d106760d34cce. Report an issue: GitHub.