{"record":{"id":"63eb442849a46b63","repo":"clockworklabs/SpacetimeDB","slug":"constraint-error-module-cleared-and-replaced-wi","errorCode":null,"errorMessage":"\n[CONSTRAINT ERROR] Module cleared and replaced with error type: %s\n","messagePattern":"\n\\[CONSTRAINT ERROR\\] Module cleared and replaced with error type: (.+?)\n","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/bindings-cpp/src/internal/Module.cpp","lineNumber":262,"sourceCode":"        \n        // Create the error type name\n        std::string error_type_name = \"ERROR_MULTIPLE_PRIMARY_KEYS_\" + g_multiple_primary_key_table_name;\n        \n        // Add a single named type export that points to a non-existent typespace index\n        // This will cause SpacetimeDB to error when it tries to resolve the type\n        RawTypeDefV10 error_type;\n        error_type.source_name.scope = {};\n        error_type.source_name.source_name = error_type_name;\n        error_type.ty = 999999; // Invalid typespace index - will cause an error\n        error_type.custom_ordering = false;\n        \n        getV10Builder().GetTypeDefs().push_back(error_type);\n        \n        // Don't add anything to the typespace - this ensures the reference is invalid\n        // The server will fail with an error message that includes our error type name\n        \n        // Also log to stderr for debugging\n        fprintf(stderr, \"\\n[CONSTRAINT ERROR] Module cleared and replaced with error type: %s\\n\", error_type_name.c_str());\n        fprintf(stderr, \"Original error: Multiple primary keys detected in table '%s'\\n\\n\", g_multiple_primary_key_table_name.c_str());\n        fflush(stderr);\n        \n        return; // Exit early, don't check type registration errors\n    }\n\n    if (g_constraint_registration_error) {\n        getV10Builder().Clear();\n\n        std::string error_type_name = \"ERROR_CONSTRAINT_REGISTRATION_\" + g_constraint_registration_error_code;\n        for (char& c : error_type_name) {\n            if (!std::isalnum(c) && c != '_') {\n                c = '_';\n            }\n        }\n\n        RawTypeDefV10 error_type;\n        error_type.source_name.scope = {};","sourceCodeStart":244,"sourceCodeEnd":280,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/6dee26c6efc2856793e12b148a59742964f5d783/crates/bindings-cpp/src/internal/Module.cpp#L244-L280","documentation":"Banner printed by __preinit__99_validate_types when g_multiple_primary_key_error was set during constraint registration. The module is cleared and replaced with the single error type ERROR_MULTIPLE_PRIMARY_KEYS_<table> at invalid typespace index 999999, so the server rejects the module description and the publish fails; this banner is the module-side explanation.","triggerScenarios":"Same condition as the 'Multiple primary keys detected' message (error 661): a second FIELD_PrimaryKey/FIELD_PrimaryKeyAutoInc constraint applied to a table that already has a primary-key column, detected in AddFieldConstraint.","commonSituations":"Composite-key SQL schemas ported verbatim; duplicated primary-key macros after copy-paste; constraints spread across multiple translation units so the clash is not obvious in one file.","solutions":["Find the table named ERROR_MULTIPLE_PRIMARY_KEYS_<table> in the banner and remove all but one primary-key macro on it","Express additional uniqueness with FIELD_Unique or a multi-column index instead","Grep every translation unit for FIELD_PrimaryKey and FIELD_PrimaryKeyAutoInc mentioning that table name"],"exampleFix":"// before\nFIELD_PrimaryKey(Order, order_id);\nFIELD_PrimaryKeyAutoInc(Order, customer_id); // second PK\n\n// after\nFIELD_PrimaryKey(Order, order_id);\nFIELD_Unique(Order, customer_id);","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["One primary-key macro per table - enforce with a CI grep","Read the ERROR_MULTIPLE_PRIMARY_KEYS_<table> suffix to identify the table instantly","Cover schema changes with a dev publish in CI"],"tags":["spacetimedb","cpp","schema","primary-key","module-init"],"backgroundTag":"multiple-primary-keys","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"}