{"record":{"id":"5b720d5434c79745","repo":"clockworklabs/SpacetimeDB","slug":"error-field-s-s-has-primary-key-constraint-ca","errorCode":null,"errorMessage":"ERROR: Field %s.%s has primary_key constraint - cannot have default value","messagePattern":"ERROR: Field (.+?)\\.(.+?) has primary_key constraint - cannot have default value","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/bindings-cpp/include/spacetimedb/internal/v9_builder.h","lineNumber":645,"sourceCode":"            field_found = true;\n            break;\n        }\n        field_idx++;\n    }\n    \n    if (!field_found) {\n        fprintf(stderr, \"ERROR: Field '%s' not found in table '%s'\\n\",\n                field_name.c_str(), table_name.c_str());\n        return;\n    }\n    \n    // Validate: default values cannot be used with primary_key, unique, or auto_inc\n    // Check if this column is in the primary key\n    for (uint16_t pk_col : table->primary_key) {\n        if (pk_col == field_idx) {\n            std::string error_msg = \"ERROR: Field \" + table_name + \".\" + field_name + \n                        \" has primary_key constraint - cannot have default value\";\n            fprintf(stderr, \"%s\", error_msg.c_str());\n            // fprintf(stderr, \"ERROR:  has primary_key constraint - cannot have default value\",\n            //          table_name.c_str(), field_name.c_str());\n            return;\n        }\n    }\n    \n    // Check if this column has a unique constraint\n    for (const auto& constraint : table->constraints) {\n        if (constraint.data.get_tag() == 0) {  // Unique constraint variant\n            const auto& unique_data = constraint.data.get<0>();\n            if (unique_data.columns.size() == 1 && unique_data.columns[0] == field_idx) {\n                fprintf(stderr, \"ERROR: Field %s.%s has unique constraint - cannot have default value\",\n                        table_name.c_str(), field_name.c_str());\n                return;\n            }\n        }\n    }\n    ","sourceCodeStart":627,"sourceCodeEnd":663,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/524b4487d949b61a07d4f39c862d1290259dfd20/crates/bindings-cpp/include/spacetimedb/internal/v9_builder.h#L627-L663","documentation":"AddColumnDefault enforces the schema rule that a column with a primary_key constraint cannot also have a default value: if the resolved field index appears in table->primary_key, it prints this message (note: without a trailing newline) and drops the default. Primary key values are identity-assigned by the database, so a client-supplied default would conflict with key generation.","triggerScenarios":"Marking a column both ST_PRIMARY_KEY/#[primary_key] and ST_COLUMN_DEFAULT on the same table; the primary key macro ran first (so table->primary_key is populated) and then AddColumnDefault resolves the same field_idx in that list.","commonSituations":"Copying a default onto an id column that was later promoted to primary key; schema migrations that add primary_key without removing existing defaults; mismatches between macro ordering making the conflict intermittent between builds.","solutions":["Remove the column default from the primary key column — keys are generated, not defaulted","If a specific key value is required, generate it in the init reducer instead of a column default","Rebuild and confirm no 'primary_key constraint - cannot have default value' message remains","Audit for the sibling checks (unique, auto_inc) on the same column"],"exampleFix":"// before\nST_TABLE(user, ...);   // id is primary key\nST_COLUMN_DEFAULT(user, id, 0);   // conflict: pk + default\n// after\nST_TABLE(user, ...);   // id is primary key, value assigned by the db\n// (default removed)","handlingStrategy":"validation","validationCode":"// Policy check before adding a default: refuse defaults on pk columns\n// (after init, with table resolved and field_idx computed):\nfor (uint16_t pk : table.primary_key)\n    if (pk == field_idx) /* skip/forbid the ST_COLUMN_DEFAULT annotation */;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Adopt a rule: identity columns (pk/auto_inc/unique) never carry defaults","Encode the rule in a schema lint step over annotations","Move seeding logic into the init reducer instead of defaults on keys"],"tags":["spacetimedb","cpp","schema","constraint-conflict","primary-key","column-default"],"backgroundTag":"schema-constraint-conflict","analyzedSha":"524b4487d949b61a07d4f39c862d1290259dfd20","analyzedAt":"2026-08-16T23:58:54.611Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}