首页 > 解决方案 > SQL Alchemy - UndefinedColumn 存在时出错

问题描述

这是我的代码:

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

这是我的数据库的所有列的图像

在此处输入图像描述

这是我检查列是否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

回溯错误:

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_...
                                                             ^


The above exception was the direct cause of the following exception:

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_...
                                                             ^

[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]
(Background on this error at: http://sqlalche.me/e/13/f405)

标签: pythonpython-3.xsqlalchemy

解决方案


推荐阅读