SQLAlchemy 获取 class 对象而不是实际结果

SQLAlchemy getting a class object instead of actual result

我运行使用自动映射对现有 Postgres 数据库进行查询。

Base = automap_base()

Base.prepare(engine, reflect=True)

City = Base.classes.city

dm = session.query(City).first()

Output: <sqlalchemy.ext.automap.city at 0x15f7a87b8>

如何设置才能看到 table 的实际结果?

来自评论:

I have used a dict to see the actual results and then turned it into a pandas df. Is there a standard way in dealing with this?

只需使用pandas'read_sql_query()方法:

Base = automap_base()
Base.prepare(autoload_with=engine)
City = Base.classes.city

query = select(City)
df = pd.read_sql_query(query, engine)
print(df)
"""
   id  city_name prov
0   1    Calgary   AB
1   2  Vancouver   BC
"""