{"record":{"id":"bb65e56d1099ddae","repo":"clockworklabs/SpacetimeDB","slug":"removeconstraint-constraint-name-not-found-in","errorCode":null,"errorMessage":"RemoveConstraint: `{constraint_name}` not found in old module def","messagePattern":"RemoveConstraint: `(.+?)` not found in old module def","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/engine/src/update.rs","lineNumber":455,"sourceCode":"                    .find(|index| index.index_name == stored_name)\n                    .ok_or_else(|| anyhow::anyhow!(\"Index `{index_name}` not found in table `{table_full_name}`\"))?;\n                let new_source_name = namespace.join_raw(&new_index_def.source_name.clone().into());\n\n                log!(\n                    logger,\n                    \"Changing index source name for `{}` on table `{}` from `{}` to `{}`\",\n                    index_name,\n                    table_full_name,\n                    index_schema.alias.as_deref().unwrap_or(\"\"),\n                    new_source_name,\n                );\n                stdb.alter_index_source_name(tx, index_schema.index_id, new_source_name)?;\n            }\n            spacetimedb_schema::auto_migrate::AutoMigrateStep::RemoveConstraint(key) => {\n                let (namespace, constraint_name) = key;\n                let (_owning_def, table_def) =\n                    plan.old.find_storing_table(namespace, constraint_name).ok_or_else(|| {\n                        anyhow::anyhow!(\"RemoveConstraint: `{constraint_name}` not found in old module def\")\n                    })?;\n                let table_full_name = joined(namespace, &table_def.name);\n                let stored_name = namespace.join_raw(&constraint_name.clone().into());\n                let table_id = stdb.table_id_from_name_mut(tx, &table_full_name)?.unwrap();\n                let table_schema = stdb.schema_for_table_mut(tx, table_id)?;\n\n                let constraint_schema = table_schema\n                    .constraints\n                    .iter()\n                    .find(|constraint| constraint.constraint_name == stored_name)\n                    .unwrap();\n\n                log!(\n                    logger,\n                    \"Dropping constraint `{constraint_name}` on table `{table_full_name}`\"\n                );\n                stdb.drop_constraint(tx, constraint_schema.constraint_id)?;\n            }","sourceCodeStart":437,"sourceCodeEnd":473,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/6dee26c6efc2856793e12b148a59742964f5d783/crates/engine/src/update.rs#L437-L473","documentation":"Applying RemoveConstraint: the diff planned dropping a constraint that exists in the database but not in the new module, yet plan.old.find_storing_table(namespace, constraint_name) cannot find its owning table in the old module def. The old-def reconstruction does not contain a constraint the database appears to have - the old def and the database's actual history disagree. The publish aborts.","triggerScenarios":"Constraint auto-names (unique, primary-key-backed) differing between the compiler build that published the database and the current one; renaming/dropping the table or constraint in the same publish as other changes; submodule constraint namespace-prefix drift.","commonSituations":"Adding or removing #[unique]/#[primary_key] plus renaming fields or tables in one publish; upgrading compiler versions between publishes; moving constrained tables into submodules.","solutions":["Clean-rebuild and republish (removes stale def artifacts).","Publish constraint changes alone - no renames, moves, or drops in the same build.","Pin constraint names where supported (e.g. #[unique(name = \"...\")]) on both module versions.","Match the toolchain version that published the live database, then migrate stepwise.","Dev: spacetime publish --delete-data <db>.","Report persistent cases with the module diff and constraint names."],"exampleFix":"// before: drop #[unique] and rename its column in one publish\npub struct Player {\n    #[spacetimedb::column(name = \"level\")] // was `hp`, still unique-keyed before\n    pub level: u32,\n}\n\n// after: two publishes\n// 1) remove #[unique] only, keep names\n// 2) rename the column separately","handlingStrategy":"fallback","validationCode":"use spacetimedb_schema::auto_migrate::AutoMigrateStep;\nfor step in &plan.steps {\n    if let AutoMigrateStep::RemoveConstraint((namespace, cname)) = step {\n        if plan.old.find_storing_table(namespace, cname).is_none() {\n            anyhow::bail!(\"old def lacks constraint {} - schema drift\", cname);\n        }\n    }\n}","typeGuard":"fn constraint_in_old_def(plan: &AutoMigratePlan, ns: &str, cname: &str) -> bool {\n    plan.old.find_storing_table(ns, cname).is_some()\n}","tryCatchPattern":"match update_database(&stdb, tx, &plan, ...).await {\n    Err(e) if e.to_string().contains(\"RemoveConstraint\")\n        && e.to_string().contains(\"old module def\") => {\n        // Fall back: publish the exact prior build first, then remove the constraint stepwise.\n    }\n    result => result?,\n}","preventionTips":["Name constraints explicitly (e.g. #[unique(name = \"...\")]) so auto-names cannot drift across compiler versions.","Publish constraint removals alone, without renames or moves.","Record the toolchain version used for each publish.","Clean-build between toolchain upgrades."],"tags":["spacetimedb","rust","schema-migration","constraint","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-14T00:17:10.932Z"}