Inb4这个问题已经被提出。这是一个常见问题,但是我遇到了独特的困难。
我要做的就是使用列名列表(作为Strings)来指定查询中选择的列。这是代码:
def _extract_db(self, db: DataBase):
"""One of the default extracts. Extracts all data from a specified tbl using the passed DB connection"""
# Build the SELECT query
q: Query = self._session(db).query(self._tbl_obj(db))
# If specific columns have been specified, only load those columns
if self._fields:
_cols = [self._column_obj(_field, db) for _field in self._fields]
q = self._session(db).query(
_cols
)
_session
,_tbl_obj
和_column_obj
函数分别返回适当的SQLAlchemy Session
,Table
和Column
对象。 Table
和Column
对象是从数据库自动映射的。 _fields
是要选择的字段列表
运行此命令时,出现错误:
InvalidRequestError: SQL expression, column, or mapped entity expected - got {...list of column objects}
因此由于某种原因,查询不喜欢_cols
我也尝试使用with_entities()
函数:
if self._fields:
_cols = [self._column_obj(_field, db) for _field in self._fields]
q = q.with_entities(_cols)
但这给了我同样的错误。我尝试将字符串列表直接传递给with_entities()
。同样的错误。我尝试用列列表定义查询:
_cols = [self._column_obj(_field, db) for _field in self._fields]
q: Query = self._session(db).query(_cols)
相同的错误。
我真的不明白。我已验证_cols
元素是SQLAlchemy Column
对象的列表,并且它们是我想要的对象,而all of是{{3} }表示我正在做什么。
有什么主意我可能做错了吗?