{"record":{"id":"59180b5c6dfb71ed","repo":"clockworklabs/SpacetimeDB","slug":"invalid-sequence-max-value-must-be-greater-than-m","errorCode":null,"errorMessage":"Invalid sequence: max_value must be greater than min_value","messagePattern":"Invalid sequence: max_value must be greater than min_value","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/datastore/src/locking_tx_datastore/sequence.rs","lineNumber":36,"sourceCode":"}\n\nimpl MemoryUsage for Sequence {\n    fn heap_usage(&self) -> usize {\n        // MEMUSE: intentionally ignoring schema\n        self.value.heap_usage()\n    }\n}\n\nimpl Sequence {\n    pub(super) fn new(schema: SequenceSchema, previous_allocation: Option<i128>) -> Self {\n        if schema.start < schema.min_value || schema.start > schema.max_value {\n            panic!(\n                \"Invalid sequence: start value {} is out of bounds for sequence with min_value {} and max_value {}\",\n                schema.start, schema.min_value, schema.max_value\n            );\n        }\n        if schema.max_value <= schema.min_value {\n            panic!(\"Invalid sequence: max_value must be greater than min_value\");\n        }\n        if schema.increment == 0 {\n            panic!(\"Invalid sequence: increment must be non-zero\");\n        }\n        if schema.increment.unsigned_abs() >= (schema.max_value - schema.min_value) as u128 {\n            panic!(\n                \"Invalid sequence: increment must be less than or equal to the range between min_value and max_value\"\n            );\n        }\n        let start = if let Some(prev) = previous_allocation {\n            if prev < schema.min_value || prev > schema.max_value {\n                // Previous versions set allocated to 0 as a default,\n                // so we have this special case.\n                if prev == 0 {\n                    schema.start\n                } else {\n                    panic!(\n                        \"Invalid sequence: previous allocation value {prev} is out of bounds for sequence with min_value {} and max_value {}\",","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/6dee26c6efc2856793e12b148a59742964f5d783/crates/datastore/src/locking_tx_datastore/sequence.rs#L18-L54","documentation":"Second invariant in Sequence::new: max_value must be strictly greater than min_value, otherwise the sequence range is empty and the constructor panics. Triggered by sequence definitions where MAXVALUE <= MINVALUE, including the equal-bounds case. It is a schema validation failure surfaced as a panic during DDL application.","triggerScenarios":"Creating a sequence with MAXVALUE equal to or less than MINVALUE, e.g. MINVALUE 10 MAXVALUE 10, or a negative MAXVALUE with a default zero MINVALUE.","commonSituations":"Copy-paste errors swapping MINVALUE/MAXVALUE clauses; programmatic schema builders that default both bounds to the same value; migrations that tighten bounds incorrectly.","solutions":["Set MAXVALUE strictly greater than MINVALUE.","Omit both clauses to accept the schema's default bounds when exact limits are not required.","Add a lint in migration tooling that asserts max > min before applying DDL."],"exampleFix":"-- before: empty range (max == min)\nCREATE SEQUENCE s MINVALUE 10 MAXVALUE 10;\n\n-- after: non-empty range\nCREATE SEQUENCE s MINVALUE 10 MAXVALUE 100;","handlingStrategy":"validation","validationCode":"fn bounds_non_empty(s: &SequenceSchema) -> Result<(), String> {\n    (s.max_value > s.min_value)\n        .then_some(())\n        .ok_or_else(|| format!(\"max_value {} must be > min_value {}\", s.max_value, s.min_value))\n}","typeGuard":"fn valid_sequence_schema(s: &SequenceSchema) -> bool {\n    s.min_value <= s.start && s.start <= s.max_value\n        && s.max_value > s.min_value\n        && s.increment != 0\n        && s.increment.unsigned_abs() < (s.max_value - s.min_value) as u128\n}","tryCatchPattern":null,"preventionTips":["Lint MINVALUE/MAXVALUE clause ordering and values in DDL review.","Omit bounds clauses when defaults suffice.","Assert max > min in programmatic schema builders before submission."],"tags":["sql","sequence","ddl","schema-validation","spacetimedb"],"backgroundTag":"sequence-definition-invalid","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"}