{"record":{"id":"d4684d47f9c2869c","repo":"clockworklabs/SpacetimeDB","slug":"error-table-s-not-found-when-trying-to-add-con","errorCode":null,"errorMessage":"ERROR: Table '%s' not found when trying to add constraint to field '%s'\n","messagePattern":"ERROR: Table '(.+?)' not found when trying to add constraint to field '(.+?)'\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/bindings-cpp/include/spacetimedb/internal/v9_builder.h","lineNumber":437,"sourceCode":"        pending_schedules_.erase(schedule_it);\n    }\n    \n    // Add the complete V9 table definition\n    AddV9Table(table_name, table_type, &typeid(T), is_public,\n               primary_key, indexes, v9_constraints, sequences, schedule);\n}\n\n// Template implementation for AddFieldConstraint\ntemplate<typename T>\nvoid V9Builder::AddFieldConstraint(const std::string& table_name,\n                                   const std::string& field_name,\n                                   FieldConstraint constraint) {\n    // AddFieldConstraint implementation\n    \n    // Find the existing table by name\n    RawTableDefV9* table = findTableByName(table_name);\n    if (!table) {\n        fprintf(stderr, \"ERROR: Table '%s' not found when trying to add constraint to field '%s'\\n\",\n                table_name.c_str(), field_name.c_str());\n        return;\n    }\n    \n    // Get field descriptors to find the field index\n    SpacetimeDB::field_registrar<T>::register_fields();\n    auto& descriptor_map = SpacetimeDB::get_table_descriptors();\n    auto it = descriptor_map.find(&typeid(T));\n    if (it == descriptor_map.end()) {\n        fprintf(stderr, \"ERROR: No field descriptors found for table %s\\n\", table_name.c_str());\n        return;\n    }\n    \n    const auto& field_descs = it->second.fields;\n    uint16_t field_idx = 0;\n    bool field_found = false;\n    \n    // Find the field index","sourceCodeStart":419,"sourceCodeEnd":455,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/524b4487d949b61a07d4f39c862d1290259dfd20/crates/bindings-cpp/include/spacetimedb/internal/v9_builder.h#L419-L455","documentation":"V9Builder::AddFieldConstraint looks up the raw table definition by name via findTableByName (exact, case-sensitive match against RawTableDefV9::name) before attaching a primary-key/unique/auto-inc constraint. When no registered table matches, it prints this error and returns, silently dropping the constraint — the module still builds, but the column ends up without the intended constraint.","triggerScenarios":"The constraint macro is instantiated with a table_name string that differs from the registered table name (typo, casing, renamed table), or the constraint registration runs before the table's own static registration in another translation unit, or the table macro and constraint macro disagree on the generated name.","commonSituations":"Renaming a table (or its ST_TABLE name argument) without updating the constraint annotation; copy-pasting constraint macros between tables and forgetting to change the name; static-init ordering differences between .cpp files causing the constraint to run first; name collision resolved by overwriting the earlier table.","solutions":["Compare the table name in the constraint registration against the exact string used in the table macro (case-sensitive)","Ensure the table's static registration object is compiled/linked such that it runs before the constraint — prefer keeping table and constraint in the same translation unit","Check for duplicate table registrations where the second one renamed/replaced the first","After fixing, republish and describe the table to confirm the constraint exists"],"exampleFix":"// before — names disagree: constraint targets 'Users', table registered as 'users'\nST_TABLE(users, ...);\nST_UNIQUE(Users, id);   // typo -> constraint silently dropped\n// after\nST_TABLE(users, ...);\nST_UNIQUE(users, id);","handlingStrategy":"validation","validationCode":"// Before relying on constraints, verify the table name resolved (after init):\n#include <spacetimedb/internal/v9_builder.h>\nbool TableIsRegistered(const std::string& name) {\n    for (const auto& t : SpacetimeDB::Internal::GetV9Module().tables)\n        if (t.name == name) return true;\n    return false;\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Derive constraint annotations from the table macro's name token instead of retyping the string","Keep a table and its constraints in one translation unit to avoid static-init ordering","After publish, describe the table and assert expected constraints exist"],"tags":["spacetimedb","cpp","schema","constraint","table-not-found","v9-builder"],"backgroundTag":"schema-table-not-found","analyzedSha":"524b4487d949b61a07d4f39c862d1290259dfd20","analyzedAt":"2026-08-16T23:58:54.611Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}