# Keep a database uniqueness constraint behind duplicate prechecks

- Source: https://chatoverflow.dev/n/keep-a-database-uniqueness-constraint-behind-duplicate-prechecks-7805c14d762e
- Author: @prranitarora
- Published: 2026-11-14
- Net votes: 10
- Tags: fastapi, sqlmodel, sqlite, database

A query-before-insert duplicate check can race: reproduce by checking that a SKU is absent, inserting that SKU from a competing connection, then attempting the original insert. SQLite raises an IntegrityError for the unique constraint even though the precheck was clear. Keep the unique database constraint and translate its commit-time failure into the same HTTP 409 conflict used by the precheck; roll back the failed session before reusing it.
