{"record":{"id":"84374e1d1e865cde","repo":"clockworklabs/SpacetimeDB","slug":"addconstraint-constraint-constraint-name-not","errorCode":null,"errorMessage":"AddConstraint: constraint `{constraint_name}` not found in new module def","messagePattern":"AddConstraint: constraint `(.+?)` not found in new module def","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/engine/src/update.rs","lineNumber":482,"sourceCode":"                    .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            }\n            spacetimedb_schema::auto_migrate::AutoMigrateStep::AddConstraint(key) => {\n                let (namespace, constraint_name) = key;\n                let (owning_def, table_def) = plan\n                    .new\n                    .find_storing_table(namespace, constraint_name)\n                    .ok_or_else(|| anyhow::anyhow!(\"AddConstraint: `{constraint_name}` not found in new module def\"))?;\n                let table_full_name = joined(namespace, &table_def.name);\n                let constraint_def: &ConstraintDef = plan.new.lookup(key).ok_or_else(|| {\n                    anyhow::anyhow!(\"AddConstraint: constraint `{constraint_name}` not found in new module def\")\n                })?;\n                let table_id = stdb\n                    .table_id_from_name_mut(tx, &table_full_name)?\n                    .expect(\"table should exist in the database for AddConstraint\");\n                let mut constraint_schema =\n                    ConstraintSchema::from_module_def(owning_def, constraint_def, table_id, ConstraintId::SENTINEL);\n\n                // Apply namespace prefix for submodule constraints\n                constraint_schema.constraint_name = namespace.join_raw(&constraint_schema.constraint_name);\n                log!(\n                    logger,\n                    \"Adding constraint `{constraint_name}` on table `{table_full_name}`\"\n                );\n                stdb.create_constraint(tx, constraint_schema)?;\n            }\n            spacetimedb_schema::auto_migrate::AutoMigrateStep::AddSequence(key) => {\n                let (namespace, sequence_name) = key;\n                let (owning_def, table_def) = plan","sourceCodeStart":464,"sourceCodeEnd":500,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/6dee26c6efc2856793e12b148a59742964f5d783/crates/engine/src/update.rs#L464-L500","documentation":"AddConstraint resolved the storing table via find_storing_table, but plan.new.lookup::<ConstraintDef>(key) returned None: the constraint is not registered as a lookupable entity in the new module def set even though a table references it. The def set's index is internally inconsistent (typically a namespace-key drift for submodule constraints). The publish aborts.","triggerScenarios":"Constraints on tables inside submodules where the constraint's registration namespace differs from the table's namespace; module defs generated by mismatched SDK/macro versions so the constraint map and the entity registry disagree.","commonSituations":"Constrained tables declared inside submodules; upgrading the spacetimedb SDK macro version between builds; partial clean builds mixing artifacts from two versions.","solutions":["Full clean rebuild (delete target/ entirely, not just the crate), spacetime build, republish.","Move the constrained table to the crate root (or a stable module path) for the publish that adds the constraint, then relocate afterwards.","Ensure exactly one SDK/macro version appears in the dependency tree (cargo tree -i spacetimedb).","Dev: spacetime publish --delete-data <db>.","Report as a def-indexing bug with the submodule layout."],"exampleFix":"// before: unique constraint on a submodule table fails to resolve in the def registry\nmod guild {\n    #[spacetimedb::table(name = \"members\")]\n    pub struct Member { #[unique] pub name: String }\n}\n\n// after: add the constraint while the table is at the root, move it in a later publish\n#[spacetimedb::table(name = \"members\")]\npub struct Member { #[unique] pub name: String }","handlingStrategy":"validation","validationCode":"use spacetimedb_schema::auto_migrate::AutoMigrateStep;\nuse spacetimedb_schema::def::ConstraintDef;\nfor step in &plan.steps {\n    if let AutoMigrateStep::AddConstraint(key) = step {\n        anyhow::ensure!(\n            plan.new.lookup::<ConstraintDef>(key).is_some(),\n            \"constraint {:?} missing from the new def registry\", key\n        );\n    }\n}","typeGuard":"fn constraint_registered(plan: &AutoMigratePlan, key: (&str, &str)) -> bool {\n    plan.new.lookup::<ConstraintDef>(key).is_some()\n}","tryCatchPattern":"match update_database(&stdb, tx, &plan, ...).await {\n    Err(e) if e.to_string().contains(\"AddConstraint: constraint\") => {\n        // Def registry desync: full clean rebuild; add the constraint at the crate root first.\n    }\n    result => result?,\n}","preventionTips":["Add constraints while the table is at a stable module path; relocate in later publishes.","Ensure a single SDK/macro version in cargo tree -i spacetimedb output.","Delete target/ fully rather than incremental builds after upgrading macros."],"tags":["spacetimedb","rust","schema-migration","constraint","submodule","namespace"],"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"}