{"record":{"id":"936f30692989abd1","repo":"clockworklabs/SpacetimeDB","slug":"error-field-s-s-has-auto-inc-constraint-canno","errorCode":null,"errorMessage":"ERROR: Field %s.%s has auto_inc constraint - cannot have default value","messagePattern":"ERROR: Field (.+?)\\.(.+?) has auto_inc constraint - cannot have default value","errorType":"console","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/bindings-cpp/include/spacetimedb/internal/v9_builder.h","lineNumber":667,"sourceCode":"        }\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    \n    // Check if this column has an auto_inc sequence\n    for (const auto& sequence : table->sequences) {\n        if (sequence.column == field_idx) {\n            fprintf(stderr, \"ERROR: Field %s.%s has auto_inc constraint - cannot have default value\",\n                    table_name.c_str(), field_name.c_str());\n            return;\n        }\n    }\n    \n    // Create the column default value structure\n    RawColumnDefaultValueV9 col_default;\n    col_default.table = table_name;\n    col_default.col_id = field_idx;\n    col_default.value = serialized_value;\n    \n    // Create the misc export entry with ColumnDefaultValue variant (variant 0)\n    RawMiscModuleExportV9 export_entry;\n    export_entry.set<0>(col_default);\n    \n    // Add to the module's misc_exports\n    GetV9Module().misc_exports.push_back(export_entry);\n}","sourceCodeStart":649,"sourceCodeEnd":685,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/524b4487d949b61a07d4f39c862d1290259dfd20/crates/bindings-cpp/include/spacetimedb/internal/v9_builder.h#L649-L685","documentation":"AddColumnDefault refuses defaults on auto-incremented columns: it scans table->sequences and finds a sequence whose column == field_idx (registered earlier by the auto_inc/ST_AUTO_INC path), prints this message and returns, dropping the default. Auto-inc columns are populated by their sequence, so a serialized default would race the sequence generator.","triggerScenarios":"A column annotated with both ST_AUTO_INC/auto_inc and ST_COLUMN_DEFAULT on the same table; the sequence registration ran first, so table->sequences contains the column when the default registration is validated.","commonSituations":"Adding a default to an id column that was already auto_inc; schema refactors merging annotations; porting from a SQL schema where DEFAULT nextval-style habits carried over.","solutions":["Remove the column default from the auto-inc column — the sequence supplies values","If a starting value is needed, set the sequence's min/start (as done in the sequence registration path) instead of a column default","Rebuild and confirm the conflict message is gone"],"exampleFix":"// before\nST_AUTO_INC(order, id);\nST_COLUMN_DEFAULT(order, id, 1000);   // auto_inc + default\n// after\nST_AUTO_INC(order, id);\n// (default removed; configure the sequence start instead)","handlingStrategy":"validation","validationCode":"// Policy check: refuse defaults on sequenced (auto_inc) columns\nfor (const auto& seq : table.sequences)\n    if (seq.column == field_idx) /* forbid the ST_COLUMN_DEFAULT annotation */;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Configure sequence start/min instead of defaults on auto-inc columns","Never combine ST_AUTO_INC and ST_COLUMN_DEFAULT on one column","Add a schema lint rule for auto_inc + default pairs"],"tags":["spacetimedb","cpp","schema","constraint-conflict","auto-increment","column-default"],"backgroundTag":"schema-constraint-conflict","analyzedSha":"524b4487d949b61a07d4f39c862d1290259dfd20","analyzedAt":"2026-08-16T23:58:54.611Z","schemaVersion":2},"datasetVersion":"2026-08-17T04:17:16.089Z"}