{"record":{"id":"1ec4b4622c8485b3","repo":"clockworklabs/SpacetimeDB","slug":"changeprimarykey-table-table-name-not-found-i","errorCode":null,"errorMessage":"ChangePrimaryKey: table `{table_name}` not found in new module def","messagePattern":"ChangePrimaryKey: table `(.+?)` not found in new module def","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/engine/src/update.rs","lineNumber":588,"sourceCode":"                let access = if let Some((_owning_def, table_def)) = plan.new.find_table(table_name_key) {\n                    table_def.table_access\n                } else {\n                    let (_owning_def, view_def) = plan.new.find_view(table_name_key).ok_or_else(|| {\n                        anyhow::anyhow!(\"ChangeAccess: `{table_name}` not found as a table or view in new module def\")\n                    })?;\n                    if view_def.is_public {\n                        TableAccess::Public\n                    } else {\n                        TableAccess::Private\n                    }\n                };\n                stdb.alter_table_access(tx, &table_name, access.into())?;\n            }\n            spacetimedb_schema::auto_migrate::AutoMigrateStep::ChangePrimaryKey(table_name_key) => {\n                let (namespace, local) = table_name_key;\n                let table_name = joined(namespace, local);\n                let (_owning_def, table_def) = plan.new.find_table(table_name_key).ok_or_else(|| {\n                    anyhow::anyhow!(\"ChangePrimaryKey: table `{table_name}` not found in new module def\")\n                })?;\n                log!(logger, \"Changing primary key for table `{table_name}`\");\n                stdb.alter_table_primary_key(tx, &table_name, table_def.primary_key)?;\n            }\n            spacetimedb_schema::auto_migrate::AutoMigrateStep::AddSchedule(_) => {\n                anyhow::bail!(\"Adding schedules is not yet implemented\");\n            }\n            spacetimedb_schema::auto_migrate::AutoMigrateStep::RemoveSchedule(_) => {\n                anyhow::bail!(\"Removing schedules is not yet implemented\");\n            }\n            spacetimedb_schema::auto_migrate::AutoMigrateStep::AddRowLevelSecurity(sql_rls) => {\n                log!(logger, \"Adding row-level security `{sql_rls}`\");\n                let rls = plan.new.lookup::<RawRowLevelSecurityDefV9>(sql_rls).ok_or_else(|| {\n                    anyhow::anyhow!(\"AddRowLevelSecurity: RLS `{sql_rls}` not found in new module def\")\n                })?;\n                let rls = RowLevelExpr::build_row_level_expr(tx, &auth_ctx, rls)?;\n\n                stdb.create_row_level_security(tx, rls.def)?;","sourceCodeStart":570,"sourceCodeEnd":606,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/6dee26c6efc2856793e12b148a59742964f5d783/crates/engine/src/update.rs#L570-L606","documentation":"Applying ChangePrimaryKey, which moves a table's primary key to another column: plan.new.find_table fails, so the new primary key cannot be read from the new def. The primary-key change was bundled with a table key that no longer resolves - plan/def desync. The publish aborts.","triggerScenarios":"Moving #[primary_key] to a different column while renaming/moving the table, or changing the PK column's stored name, in the same publish; def/plan desync from stale artifacts or version mismatch.","commonSituations":"Re-keying tables (e.g. from auto_inc id to a composite/natural key) during broader refactors; renaming the PK column in the same commit as the re-key.","solutions":["Clean-rebuild and republish.","Publish the primary-key change alone: same table name, same column names.","Pin table and column names with #[table(name = ...)] / #[column(name = ...)].","Align SDK/CLI/server versions with the live database.","Dev: spacetime publish --delete-data <db> (recreating loses rows; export first if needed).","Report as a planner bug if a clean single-change publish fails."],"exampleFix":"// before: change #[primary_key] column AND rename the table in one publish\n#[spacetimedb::table(name = \"members\")] // was `players`\npub struct Member { #[primary_key] pub name: String, pub id: u64 /* was PK */ }\n\n// after: two publishes\n// 1) re-key keeping name `players`\n#[spacetimedb::table(name = \"players\")]\npub struct Player { #[primary_key] pub name: String, pub id: u64 }\n// 2) rename the table separately","handlingStrategy":"validation","validationCode":"use spacetimedb_schema::auto_migrate::AutoMigrateStep;\nfor step in &plan.steps {\n    if let AutoMigrateStep::ChangePrimaryKey(key) = step {\n        if plan.new.find_table(key).is_none() {\n            anyhow::bail!(\"ChangePrimaryKey step references table {:?} missing from new def\", key);\n        }\n    }\n}","typeGuard":"fn pk_table_in_new_def(plan: &AutoMigratePlan, key: (&str, &str)) -> bool {\n    plan.new.find_table(key).is_some()\n}","tryCatchPattern":"match update_database(&stdb, tx, &plan, ...).await {\n    Err(e) if e.to_string().contains(\"ChangePrimaryKey\") => {\n        // Split: re-key with a stable table/column naming; rename in a follow-up publish.\n    }\n    result => result?,\n}","preventionTips":["Re-key tables in a publish with no renames of the table or PK column.","Pin table and column names before changing #[primary_key].","Export data before re-keying, since fallbacks may require --delete-data.","Clean-build and align toolchain versions before the migration."],"tags":["spacetimedb","rust","schema-migration","primary-key","publish"],"backgroundTag":"schema-migration-plan-mismatch","analyzedSha":"6dee26c6efc2856793e12b148a59742964f5d783","analyzedAt":"2026-08-20T06:08:37.179Z","contentChangedAt":"2026-08-20T06:08:37.179Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}