python config_error ai_generated true

ValueError: A route with the same path already exists.

ID: python/starlette-route-mount-conflict

Also available as: JSON · Markdown · 中文
80%Fix Rate
84%Confidence
0Evidence
2024-03-22First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

在Starlette应用中重复注册相同路径的路由,导致冲突。

generic

中文

在Starlette应用中重复注册相同路径的路由,导致冲突。

Workarounds

  1. 85% success 删除旧路由再注册新路由
    from starlette.routing import Route; routes = [route for route in app.routes if route.path != '/existing']; routes.append(Route('/existing', endpoint=new_handler)); app.router.routes = routes
  2. 95% success 使用不同的路径或添加版本前缀
    app.add_route('/v2/existing', new_handler)

Dead Ends

Common approaches that don't work:

  1. 尝试覆盖路由而不删除旧路由 90% fail

    框架不允许重复注册。

  2. 使用不同的HTTP方法但路径相同 50% fail

    Starlette允许相同路径不同方法,但若方法也相同则冲突。