{"success":true,"name":"use-a-version-column-for-sql-updates-from-edit-forms","description":"Two users can edit and save the same SQL record.","category":"ai-brimind","path":"ai-brimind/use-a-version-column-for-sql-updates-from-edit-forms/SKILL.md","file":"SKILL.md","content":"---\nname: use-a-version-column-for-sql-updates-from-edit-forms\ndescription: \"Two users can edit and save the same SQL record.\"\n---\n\n# Use a Version Column for SQL Updates From Edit Forms\n\n## When to use\nProject uses SQL, and users load a record into a form, edit it, and save later — and a second user could save in between.\n\n## Example request\nTwo admins edit the same order → the second save silently erases the first's changes; last-write-wins was never a decision, just an accident. Holding a database lock while the form is open → locks and human think-time don't mix; locks are for milliseconds, not minutes. A version (or updated_at) column checked in the UPDATE's WHERE → zero rows affected means someone else saved first; that's optimistic locking, the standard pattern. Conflict detected → the app can show 'this record changed, review before saving'; the user decides, not chance.\n\n## Instructions\nAct like a developer who knows humans edit slowly and databases can't hold their breath that long. For edit-and-save flows on shared records: use optimistic locking — add a version integer (or use updated_at), send it to the form, and write back with UPDATE ... SET ..., version = version + 1 WHERE id = ? AND version = ?. Check the affected row count: zero means another save happened first, and the app must surface the conflict instead of overwriting silently. Never hold a database lock across user think-time. Check whether your ORM ships optimistic locking built in before hand-rolling it — most do. Only ship once you can describe exactly what each of two concurrent editors sees, and confirm no one's changes can vanish without a warning.\n\n## Metadata\n- role: developer\n- level: tactical\n- places: vibeCoding\n- origin: ai.brimind.pro skill #1122\n","linked_files":[],"origin":"ai.brimind.pro","owner_id":null,"source":"router"}