{"record":{"id":"f2bd8c6e63b061ed","repo":"tiangolo/fastapi","slug":"item-already-exists-f2bd8c","errorCode":null,"errorMessage":"Item already exists","messagePattern":"Item already exists","errorType":"http","errorClass":"HTTPException","httpStatus":409,"severity":"error","filePath":"docs_src/app_testing/app_b_py310/main.py","lineNumber":34,"sourceCode":"    title: str\n    description: str | None = None\n\n\n@app.get(\"/items/{item_id}\", response_model=Item)\nasync def read_main(item_id: str, x_token: str = Header()):\n    if x_token != fake_secret_token:\n        raise HTTPException(status_code=400, detail=\"Invalid X-Token header\")\n    if item_id not in fake_db:\n        raise HTTPException(status_code=404, detail=\"Item not found\")\n    return fake_db[item_id]\n\n\n@app.post(\"/items/\")\nasync def create_item(item: Item, x_token: str = Header()) -> Item:\n    if x_token != fake_secret_token:\n        raise HTTPException(status_code=400, detail=\"Invalid X-Token header\")\n    if item.id in fake_db:\n        raise HTTPException(status_code=409, detail=\"Item already exists\")\n    fake_db[item.id] = item.model_dump()\n    return item\n","sourceCodeStart":16,"sourceCodeEnd":37,"githubUrl":"https://github.com/tiangolo/fastapi/blob/3e8d1526d83a90aaf7d6eb6dc682bf150f180b25/docs_src/app_testing/app_b_py310/main.py#L16-L37","documentation":"POST /items/ duplicate-id check (409) in the legacy-style file: raises when item.id is already a key in fake_db. Pre-seeded ids 'foo' and 'bar' always conflict; any id POSTed twice in one process conflicts on the second call.","triggerScenarios":"POST /items/ with id 'foo' or 'bar', or POSTing the same id twice.","commonSituations":"Retried POSTs after a client timeout; fixed/test ids that collide with seed data.","solutions":["Use a fresh unique id per create (uuid4).","Handle 409 as idempotent success when the stored item matches.","Switch to PUT for upsert behavior."],"exampleFix":"// before\nPOST /items/  {\"id\":\"foo\", ...}\n// after\nPOST /items/  {\"id\": str(uuid4()), ...}","handlingStrategy":"try-catch","validationCode":"import uuid, httpx\ndef create(item: dict):\n    item = {**item, 'id': str(uuid.uuid4())}\n    return httpx.post('http://localhost:8000/items/', json=item, headers={'X-Token': 'coneofsilence'})","typeGuard":"def is_unique_id(proposed: str, existing: set[str]) -> bool:\n    return proposed not in existing","tryCatchPattern":"try:\n    resp = httpx.post(url, json=item, headers=headers)\n    resp.raise_for_status()\nexcept httpx.HTTPStatusError as e:\n    if e.response.status_code == 409:\n        ...","preventionTips":["Use uuid4 ids to avoid collisions with seed data.","Handle 409 as idempotent success when applicable.","Track created ids in the client session."],"tags":["fastapi","conflict","duplicate","httpexception","app-testing"],"backgroundTag":null,"analyzedSha":"3e8d1526d83a90aaf7d6eb6dc682bf150f180b25","analyzedAt":"2026-08-11T02:34:52.986Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}