{"record":{"id":"2e23f0b24865357e","repo":"clockworklabs/SpacetimeDB","slug":"precheck-failed-added-sequence-sequence-name-al","errorCode":null,"errorMessage":"Precheck failed: added sequence {sequence_name} already has values in range","messagePattern":"Precheck failed: added sequence (.+?) already has values in range","errorType":"validation","errorClass":"anyhow::Error","httpStatus":null,"severity":"error","filePath":"crates/engine/src/update.rs","lineNumber":257,"sourceCode":"                    .ok_or_else(|| {\n                        anyhow::anyhow!(\"Precheck failed: added sequence {sequence_name} has invalid min value\")\n                    })?;\n\n                let max = match sequence_def.max_value {\n                    Some(max) => ty.saturating_value_from_i128(max),\n                    None => ty.saturating_value_from_i128(i128::MAX),\n                }\n                .ok_or_else(|| {\n                    anyhow::anyhow!(\"Precheck failed: added sequence {sequence_name} has invalid max value\")\n                })?;\n\n                let range = min..=max;\n                if stdb\n                    .iter_by_col_range_mut(tx, table_id, sequence_def.column, range)?\n                    .next()\n                    .is_some()\n                {\n                    anyhow::bail!(\"Precheck failed: added sequence {sequence_name} already has values in range\",);\n                }\n            }\n        }\n    }\n\n    log::info!(\"Running database update steps: {}\", stdb.database_identity());\n    let mut res = UpdateResult::Success;\n\n    for step in plan.steps {\n        match step {\n            spacetimedb_schema::auto_migrate::AutoMigrateStep::RemoveTable(table_name_key) => {\n                let (namespace, local) = table_name_key;\n                let table_name = joined(namespace, local);\n                let table_id = stdb.table_id_from_name_mut(tx, &table_name)?.unwrap();\n\n                if stdb.table_row_count_mut(tx, table_id).unwrap_or(0) > 0 {\n                    anyhow::bail!(\n                        \"Cannot remove table `{table_name}`: table contains data. \\","sourceCodeStart":239,"sourceCodeEnd":275,"githubUrl":"https://github.com/clockworklabs/SpacetimeDB/blob/6dee26c6efc2856793e12b148a59742964f5d783/crates/engine/src/update.rs#L239-L275","documentation":"During auto-migration, when a plan step adds a sequence (auto_inc) to an existing table, a precheck iterates rows whose value in that column falls within the sequence's [min..=max] range. If any existing row already holds a value the sequence would generate, migration aborts to prevent future primary-key/ID collisions.","triggerScenarios":"Adding #[auto_inc]/a sequence to a column of a table that already contains rows with values inside the new sequence's range -- e.g. ids 1..50 exist and the sequence starts at 1; or seeded/imported data written with explicit ids before the sequence is introduced.","commonSituations":"Retrofitting auto_inc onto a manually populated table; importing seed data then adding the sequence in the same release; a migration that re-adds a sequence after data was written by explicit-id inserts.","solutions":["Check the current maximum (SELECT MAX(col) FROM t) and define the sequence to start strictly above it","Or delete/backfill the conflicting rows so none fall inside the sequence range, then re-run the migration","If the ids are disposable, clear the table before adding the sequence","Prefer an explicit, reviewed migration over auto-migrate when the table already holds production data"],"exampleFix":"-- before: rows with id 1..50 exist, sequence starts at 1 -> precheck fails\nALTER TABLE my_table ... auto_inc on id;\n\n-- after: start beyond the current max, or clear the data first\nSELECT MAX(id) FROM my_table;  -- 50\nCREATE SEQUENCE my_seq START 51;\n-- or: DELETE FROM my_table; then re-run the migration","handlingStrategy":"validation","validationCode":"-- before adding a sequence to an existing column\nspacetime sql my-db 'SELECT MAX(id) FROM my_table';\n-- the new sequence must start strictly above this value","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Add auto_inc at table creation time, not retroactively on populated tables","Before any migration that adds a sequence, compute MAX(col) and set the start above it","Keep explicit-id inserts out of columns destined for sequence management","Use explicit reviewed migrations (spacetime migrate) for tables with production data"],"tags":["migration","schema","sequences","auto-inc","data-integrity"],"backgroundTag":"auto-migration-precheck-failed","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"}