python config_error ai_generated true

ValueError: 重复的路由路径:/items

ValueError: Duplicate route path: /items

ID: python/fastapi-valueerror-duplicate-route-path

其他格式: JSON · Markdown 中文 · English
80%修复率
86%置信度
0证据数
2024-05-14首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

两个路由装饰器定义了相同的路径而没有区分方法。

English

Two route decorators define the same path without differentiating methods.

generic

解决方案

  1. 95% 成功率 Use different HTTP methods for same path
    @app.get('/items')
    async def list_items(): pass
    @app.post('/items')
    async def create_item(): pass
  2. 90% 成功率 Use different paths
    @app.get('/items')
    async def list_items(): pass
    @app.get('/items/{item_id}')
    async def get_item(item_id: int): pass

无效尝试

常见但无效的做法:

  1. Removing one route entirely 50% 失败

    May lose needed functionality.

  2. Changing function name only 70% 失败

    Path remains duplicate.