SQL Alchemy存在时未定义列错误

2024-09-29 21:45:20 发布

您现在位置:Python中文网/ 问答频道 /正文

这是我的密码:

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

这是包含所有列的数据库的图像

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

回溯错误:

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)

Tags: inpyexecutesqlalchemylocalusersappdatarouting

热门问题