{"schemaVersion":"1.0","canonicalUrl":"https://automationtester.in/failures/test-data-unique-constraint","slug":"test-data-unique-constraint","title":"Unique constraint failed in a test","signature":"Unique constraint failed on the fields","frameworks":["pytest","Jest","CI"],"classification":"test-data","summary":"The test attempted to create a record whose unique value already exists in the database visible to that test.","causes":[{"title":"Parallel tests share fixed fixture values","explanation":"Two workers create the same email, slug, key, or external id.","diagnostic":"Compare worker IDs and the colliding field across concurrent tests.","fix":"Generate a deterministic worker-scoped value and keep assertions independent of the literal."},{"title":"Cleanup did not run after an earlier failure","explanation":"A previous test left data that the next run assumes is absent.","diagnostic":"Query the record before setup and inspect teardown failures.","fix":"Use transaction rollback, isolated schemas, or idempotent setup and teardown."},{"title":"A retry repeats a non-idempotent setup","explanation":"The retried test creates the same record without resetting state.","diagnostic":"Check whether setup is inside the retried boundary.","fix":"Make setup idempotent or create fresh retry-scoped data."}],"prevention":["Give each worker isolated data.","Use transactional cleanup.","Make retry setup idempotent."],"related":["database-pool-exhausted","pytest-fixture-not-found"],"references":[{"label":"PostgreSQL unique constraints","url":"https://www.postgresql.org/docs/current/ddl-constraints.html#DDL-CONSTRAINTS-UNIQUE-CONSTRAINTS"}]}