You need to enable JavaScript to run this app. SQLAlchemy 报 UndefinedColumn 错误:模型已定义 is_hub 列但查询提示不存在
优惠活动
大模型
产品
解决方案
定价
更多

SQLAlchemy 报 UndefinedColumn 错误:模型已定义 is_hub 列但查询提示不存在

相关代码

以下是我编写的SQLAlchemy模型与查询逻辑:

class Handover_Point(Base):
    __tablename__ = 'r_handover_point'
    id = Column(UUID(as_uuid=True), primary_key=True)
    point = Column(String(255))
    name = Column(String(255))
    short_id = Column(String(255))
    address = Column(String(255))
    geo = Column(Geometry('POINT', spatial_index=False))
    pic_name = Column(String(255))
    mobile = Column(String(255))
    email = Column(String(255))
    is_active = Column(Boolean)
    is_hub = Column(Boolean)
    __table_args__ = (
        Index('idx_hp_geo', 'geo', postgresql_using='gist'),
        {'schema': 'routing'},
    )


rule = Session.query(Handover_Point.name,
                     Handover_Point.pic_name,
                     Handover_Point.address,
                     Handover_Point.is_hub).all()

运行异常

执行上述代码时,控制台抛出如下报错:

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedColumn) column r_handover_point.is_hub does not exist
LINE 1: ...oint.address AS routing_r_handover_point_address, routing.r

实际表结构

数据库中r_handover_point表的全部列结构如下(附截图):
enter image description here

字段校验

我写了一段代码,专门校验is_hub字段是否被模型正确识别:

from sqlalchemy import inspect
mapper = inspect(Handover_Point)
for column in mapper.attrs:
    print(column.key)

运行这段校验代码的输出结果为:

id
point
name
short_id
address
geo
pic_name
mobile
email
is_active
is_hub

可以看到模型层面已经把is_hub字段正确识别出来了,和代码里定义的列完全对得上,但实际查询还是报列不存在的错。

完整错误堆栈

Traceback (most recent call last):
  File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\base.py", line 1276, in _execute_context
    self.dialect.do_execute(
  File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\default.py", line 593, in do_execute
    cursor.execute(statement, parameters)
psycopg2.errors.UndefinedColumn: column r_handover_point.is_hub does not exist
LINE 1: ...oint.address AS routing_r_handover_point_address, routing.r_...
                                                             ^

上述异常是触发后续错误堆栈的直接原因:

Traceback (most recent call last):
  File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\test.py", line 39, in <module>
    rule = Session.query(Handover_Point.name,
  File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\orm\query.py", line 3373, in all
    return list(self)
  File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\orm\query.py", line 3535, in __iter__
    return self._execute_and_instances(context)
  File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\orm\query.py", line 3560, in _execute_and_instances
    result = conn.execute(querycontext.statement, self._params)
  File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\base.py", line 1011, in execute
    return meth(self, multiparams, params)
  File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\sql\elements.py", line 298, in _execute_on_connection
    return connection._execute_clauseelement(self, multiparams, params)
  File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\base.py", line 1124, in _execute_clauseelement
    ret = self._execute_context(
  File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\base.py", line 1316, in _execute_context
    self._handle_dbapi_exception(
  File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\base.py", line 1510, in _handle_dbapi_exception
    util.raise_(
  File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\util\compat.py", line 182, in raise_
    raise exception
  File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\base.py", line 1276, in _execute_context
    self.dialect.do_execute(
  File "C:\Users\Loi Chau\AppData\Local\Programs\Python\Python39\lib\site-packages\sqlalchemy\engine\default.py", line 593, in do_execute
    cursor.execute(statement, parameters)
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedColumn) column r_handover_point.is_hub does not exist
LINE 1: ...oint.address AS routing_r_handover_point_address, routing.r_...
                                                             ^

ORM最终拼接生成的实际执行SQL:
```sql
SELECT 
  routing.r_handover_point.name AS routing_r_handover_point_name, 
  routing.r_handover_point.pic_name AS routing_r_handover_point_pic_name, 
  routing.r_handover_point.address AS routing_r_handover_point_address, 
  routing.r_handover_point.is_hub AS routing_r_handover_point_is_hub 
FROM routing.r_handover_point

内容的提问来源于stack exchange,提问作者Chau Loi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.11.10 01:45:07