python config_error ai_generated true

sqlalchemy.exc.UnboundExecutionError: Could not locate a bind configured on mapper, SQL expression, or this Session.

ID: python/sqlalchemy-missing-session-bind

Also available as: JSON · Markdown · 中文
80%Fix Rate
89%Confidence
0Evidence
2025-05-16First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
1.4.x active
2.0.x active

Root Cause

会话或引擎未正确绑定到数据库,无法执行 SQL。

generic

中文

会话或引擎未正确绑定到数据库,无法执行 SQL。

Workarounds

  1. 95% success
    创建会话时绑定引擎:Session = sessionmaker(bind=engine)
  2. 85% success
    在查询时指定 bind:session.execute(stmt, bind=engine)

Dead Ends

Common approaches that don't work:

  1. 80% fail

    尝试使用默认会话,但未配置引擎。

  2. 50% fail

    在会话中手动添加 bind,但可能与其他配置冲突。