{"record":{"id":"a4fc2e08d66c476a","repo":"BerriAI/litellm","slug":"foreign-key-constraint-failed-organization-id-db","errorCode":null,"errorMessage":"Foreign Key Constraint failed. Organization ID={db_data['organization_id']} does not exist in LiteLLM_OrganizationTable. Create via `/organization/new`.","messagePattern":"Foreign Key Constraint failed\\. Organization ID=(.+?) does not exist in LiteLLM_OrganizationTable\\. Create via `/organization/new`\\.","errorType":"http","errorClass":"HTTPException","httpStatus":400,"severity":"error","filePath":"litellm/proxy/utils.py","lineNumber":4021,"sourceCode":"                )\n                verbose_proxy_logger.info(\"Data Inserted into Keys Table\")\n                return new_verification_token\n            elif table_name == \"user\":\n                db_data = self.jsonify_object(data=data)\n                try:\n                    new_user_row: Final = await UserRepository(self).table.upsert(\n                        where={\"user_id\": data[\"user_id\"]},\n                        data={\n                            \"create\": {**db_data},\n                            \"update\": {},  # don't do anything if it already exists\n                        },\n                    )\n                except Exception as e:\n                    if (\n                        \"Foreign key constraint failed on the field: `LiteLLM_UserTable_organization_id_fkey (index)`\"\n                        in str(e)\n                    ):\n                        raise HTTPException(\n                            status_code=400,\n                            detail={\n                                \"error\": f\"Foreign Key Constraint failed. Organization ID={db_data['organization_id']} does not exist in LiteLLM_OrganizationTable. Create via `/organization/new`.\"\n                            },\n                        )\n                    raise e\n                verbose_proxy_logger.info(\"Data Inserted into User Table\")\n                return new_user_row\n            elif table_name == \"team\":\n                db_data = self.jsonify_team_object(db_data=data)\n                new_team_row: Final = await TeamRepository(self).table.upsert(\n                    where={\"team_id\": data[\"team_id\"]},\n                    data={\n                        \"create\": {**db_data},\n                        \"update\": {},  # don't do anything if it already exists\n                    },\n                )\n                verbose_proxy_logger.info(\"Data Inserted into Team Table\")","sourceCodeStart":4003,"sourceCodeEnd":4039,"githubUrl":"https://github.com/BerriAI/litellm/blob/77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8/litellm/proxy/utils.py#L4003-L4039","documentation":"Foreign-key violation on inserting/upserting a user (or related row) whose organization_id has no matching row in LiteLLM_OrganizationTable. The database rejected the write because the referenced organization does not exist; create it first via POST /organization/new, then retry.","triggerScenarios":"Thrown at litellm/proxy/utils.py:4021 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Create the organization first via /organization/new, or remove/correct the organization_id in the request."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"77b7c6c40c0c5aa5fbcb1d6a1825ac39ca8829b8","analyzedAt":"2026-08-18T11:44:31.656Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}