{"success":true,"name":"never-rely-on-sql-auto-increment-ids-being-gapless-or-ordere","description":"Logic assumes SQL ids are consecutive or count records.","category":"ai-brimind","path":"ai-brimind/never-rely-on-sql-auto-increment-ids-being-gapless-or-ordere/SKILL.md","file":"SKILL.md","content":"---\nname: never-rely-on-sql-auto-increment-ids-being-gapless-or-ordere\ndescription: \"Logic assumes SQL ids are consecutive or count records.\"\n---\n\n# Never Rely on SQL Auto-Increment IDs Being Gapless or Ordered\n\n## When to use\nProject uses SQL, and code or a feature treats auto-increment ids as sequence numbers — counting them, expecting no gaps, or using max(id) as 'the latest'.\n\n## Example request\nInvoice number taken from the row id → a rolled-back insert leaves a gap and accounting panics; sequences skip on rollback by design. 'Total users = max(id)' → deletions and gaps make it fiction; COUNT(*) answers that question. Assuming higher id means created later → replication, imports, and resets can break it; created_at is the time truth. Gapless legal numbering needed → that's its own pattern (a counter row updated in a transaction); the auto-increment was never built for it.\n\n## Instructions\nAct like a developer who knows what the database promises and refuses to build on what it doesn't. Auto-increment ids guarantee uniqueness — nothing else: rollbacks, failed inserts, and restarts create gaps; ordering by id is not reliably ordering by time. Audit the logic you're writing: if it counts via ids, detects 'missing' records by gaps, or derives business numbers (invoices, tickets) from row ids, it's built on a false promise. Use COUNT(*) for counts, created_at for time order, and for genuinely gapless legal numbering implement an explicit counter updated atomically in a transaction — that requirement is real but separate from the primary key. Only proceed once nothing in the feature depends on a guarantee the id column never made.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1124\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}