0

我想投一些列,然后选择所有其他列

id, name, property, description = column("id"), column("name"), column("property"), column("description")

select([cast(id, String).label('id'), cast(property, String).label('property'), name, description]).select_from(events_table)

有什么方法可以转换某些列并全选而不提及所有列名

我试过了

select([cast(id, String).label('id'), cast(property, String).label('property')], '*').select_from(events_table)

py_.transform(return_obj, lambda acc, element: acc.append(dict(element)), [])

但是我得到了两个额外的列(总共 7 列),它们被强制转换,我无法将它们转换为会引发键错误的字典。

我正在使用 FASTAPI、sqlalchemy 和数据库(异步)

谢谢

4

1 回答 1

0

很确定你能做到

select_columns = []
for field in events_table.keys()
    select_columns.append(getattr(events_table.c, field))

select(select_columns).select_from(events_table)

从该表中选择所有字段。您还可以保留要实际选择的字段列表,而不是 events_table.keys(),例如

select_these = ["id", "name", "property", "description"]
select_columns = []
for field in select_these
    select_columns.append(getattr(events_table.c, field))

select(select_columns).select_from(events_table)
于 2019-12-20T11:57:05.243 回答