{"record":{"id":"a15dd285b0f8cfd6","repo":"cocoindex-io/cocoindex","slug":"failed-to-add-column-col-name-r-to-sqlite-table","errorCode":null,"errorMessage":"Failed to add column {col_name!r} to SQLite table {table_name!r}: {e}","messagePattern":"Failed to add column (.+?) to SQLite table (.+?): (.+?)","errorType":"exception","errorClass":"RuntimeError","httpStatus":null,"severity":"error","filePath":"python/cocoindex/connectors/sqlite/_target.py","lineNumber":848,"sourceCode":"        if desired_col is None:\n            # If the desired schema no longer mentions this column, treat\n            # it as a no-op here; \"delete\" should have been emitted.\n            continue\n\n        if action in (\"insert\", \"upsert\"):\n            # SQLite supports ADD COLUMN\n            nullable = \"\" if desired_col.nullable else \" NOT NULL\"\n            try:\n                conn.execute(\n                    f\"ALTER TABLE {qualified_name} \"\n                    f'ADD COLUMN \"{col_name}\" {desired_col.type}{nullable}'\n                )\n            except sqlite3.OperationalError as e:\n                # Only ignore if column already exists (e.g. upsert / idempotent re-run)\n                if \"duplicate column name\" in str(e).lower():\n                    pass\n                else:\n                    raise RuntimeError(\n                        f\"Failed to add column {col_name!r} to SQLite table {table_name!r}: {e}\"\n                    ) from e\n            continue\n\n        if action == \"replace\":\n            # SQLite doesn't support ALTER COLUMN TYPE directly.\n            # For type changes, attempt to drop and re-add column.\n            try:\n                conn.execute(f'ALTER TABLE {qualified_name} DROP COLUMN \"{col_name}\"')\n            except sqlite3.OperationalError:\n                pass\n            nullable = \"\" if desired_col.nullable else \" NOT NULL\"\n            try:\n                conn.execute(\n                    f\"ALTER TABLE {qualified_name} \"\n                    f'ADD COLUMN \"{col_name}\" {desired_col.type}{nullable}'\n                )\n            except sqlite3.OperationalError as e:","sourceCodeStart":830,"sourceCodeEnd":866,"githubUrl":"https://github.com/cocoindex-io/cocoindex/blob/e84aa99b3292c5270a4b313b2a7137ad9ce8ab3b/python/cocoindex/connectors/sqlite/_target.py#L830-L866","documentation":"When applying an `add` column action, `_apply_column_actions` runs `ALTER TABLE ... ADD COLUMN`. If SQLite raises `OperationalError` for any reason other than 'duplicate column name' (which is tolerated as idempotent re-run), the error is wrapped in a `RuntimeError` naming the column and table.","triggerScenarios":"Adding a column whose SQLite type is invalid or unsupported; attempting to add a non-nullable PRIMARY KEY/UNIQUE column (SQLite restriction); the table name colliding with an internal alias or a reserved name; a concurrently dropped table.","commonSituations":"Schema evolution where the new column's derived SqliteType is malformed (e.g. a bad vector dimension); adding a column declared `nullable=False` with a PRIMARY KEY constraint; running two syncs concurrently on the same database file.","solutions":["Read the wrapped SQLite message (`{e}`) to identify the underlying ALTER TABLE failure","Make the new column nullable or provide a default so SQLite's ADD COLUMN restrictions are satisfied","Fix the column type mapping (SqliteType / VectorSchemaProvider override) to a valid SQLite type","Ensure no concurrent process is mutating the same table during sync"],"exampleFix":"// before (column override forcing invalid/strict add)\ncolumn_overrides={\"embedding\": sqlite.SqliteType.FLOAT_VECTOR(dim=0)}\n// after\ncolumn_overrides={\"embedding\": sqlite.VectorSchemaProvider(dim=768)}","handlingStrategy":"try-catch","validationCode":"# validate override types before sync\nfor name, ov in column_overrides.items():\n    assert ov is not None and name in schema_fields","typeGuard":null,"tryCatchPattern":"try:\n    await app.update()\nexcept RuntimeError as e:\n    if str(e).startswith(\"Failed to add column\"):\n        log_sqlite_detail(e.__cause__)\n        # fix column type/nullability, then retry\n    else:\n        raise","preventionTips":["Keep record field types mappable to valid SQLite types","Make newly added columns nullable or defaulted","Avoid concurrent writers on the same SQLite file during sync"],"tags":["sqlite","alter-table","schema-migration","sql"],"backgroundTag":"database-write-failed","analyzedSha":"e84aa99b3292c5270a4b313b2a7137ad9ce8ab3b","analyzedAt":"2026-09-08T15:59:19.997Z","contentChangedAt":"2026-09-08T15:59:19.997Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}