python config_error ai_generated true

AssertionError: Path /items conflicts with existing route

ID: python/starlette-route-duplicate

Also available as: JSON · Markdown · 中文
80%Fix Rate
83%Confidence
0Evidence
2024-11-20First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

在Starlette应用中定义了多个相同路径的路由,导致冲突。

generic

中文

在Starlette应用中定义了多个相同路径的路由,导致冲突。

Workarounds

  1. 95% success
    合并路由或使用不同路径:
    @app.route("/items")
    async def list_items(request): ...
    @app.route("/items/{item_id}")
    async def get_item(request): ...
  2. 90% success
    使用路由前缀:
    app = Starlette(routes=[
        Route("/api/items", endpoint=...),
        Route("/api/items/{id}", endpoint=...)
    ])

Dead Ends

Common approaches that don't work:

  1. 60% fail

    尝试使用不同的方法(GET/POST)但路径相同,仍会冲突。

  2. 80% fail

    忽略冲突,但路由匹配可能意外。