python - 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)
解决方案
推荐阅读
- java - Eclipse JUnit assertEquals(String, String) - 在失败跟踪中禁用方括号差异
- python - 通过 Paramiko exec_command 运行命令时,它会添加一些其他数据 (\xe2\x80\x98/tmp/filename.txt\xe2\x80\x99)
- c# - 仅将模式窗口显示到活动选项卡
- java - Recyclerview 和适配器不显示
- go - Pointer values rewritten in struct when appending to slice struct
- excel - 如何在Excel中创建复选框
- angular - 为什么我的子组件的加载微调器从未显示?
- c - 在c中没有功能的拆分单词
- pandas - 如何为 Pandas DataFrame 中的每个唯一 ID 创建项目列表?
- string - 如何将每个句子的第一个单词的TextField值首字母大写?