session.execute returns 模型对象而不是实际数据
session.execute returns model objects instead of actual data
我已经从 connection.execute
切换到 session.execute
。我无法从中获取可用数据。结果似乎包含对模型的引用而不是实际的行数据。
with Session(engine) as s:
q = select(WarrantyRequest)
res = s.execute(q)
keys = res.keys()
data_list = res.all()
print(keys) # should print list of column names
print(data_list) # should print list of lists with row data
dict_list = s.execute(q).mappings().all()
print(dict_list) # should print list of dicts with column names as keys
它打印
RMKeyView(['WarrantyRequest'])
[(<models.mock.WarrantyRequest object at 0x7f4d065d3df0>,), ...]
[{'WarrantyRequest': <models.mock.WarrantyRequest object at 0x7f002b464df0>}, ... ]
当对 connection.execute
做同样的事情时,我得到了预期的结果。
我错过了什么?
docs 中有一段描述了这种行为,但我无法说出我应该如何从中获取数据。
It’s important to note that while methods of Query such as Query.all() and Query.one() will return instances of ORM mapped objects directly in the case that only a single complete entity were requested, the Result object returned by Session.execute() will always deliver rows (named tuples) by default; this is so that results against single or multiple ORM objects, columns, tables, etc. may all be handled identically.
If only one ORM entity was queried, the rows returned will have exactly one column, consisting of the ORM-mapped object instance for each row. To convert these rows into object instances without the tuples, the Result.scalars() method is used to first apply a “scalars” filter to the result; then the Result can be iterated or deliver rows via standard methods such as Result.all(), Result.first(), etc.
查询模型
q = select(WarrantyRequest)
returns 行,由各个模型实例组成。要改为获取原始数据行,请查询模型的 __table__
属性:
q = select(WarrantyRequest.__table__)
SQLAlchemy 的 ORM 层以面向对象的方式呈现数据库表和行,假设程序员希望使用对象及其属性而不是原始数据。
我已经从 connection.execute
切换到 session.execute
。我无法从中获取可用数据。结果似乎包含对模型的引用而不是实际的行数据。
with Session(engine) as s:
q = select(WarrantyRequest)
res = s.execute(q)
keys = res.keys()
data_list = res.all()
print(keys) # should print list of column names
print(data_list) # should print list of lists with row data
dict_list = s.execute(q).mappings().all()
print(dict_list) # should print list of dicts with column names as keys
它打印
RMKeyView(['WarrantyRequest'])
[(<models.mock.WarrantyRequest object at 0x7f4d065d3df0>,), ...]
[{'WarrantyRequest': <models.mock.WarrantyRequest object at 0x7f002b464df0>}, ... ]
当对 connection.execute
做同样的事情时,我得到了预期的结果。
我错过了什么?
docs 中有一段描述了这种行为,但我无法说出我应该如何从中获取数据。
It’s important to note that while methods of Query such as Query.all() and Query.one() will return instances of ORM mapped objects directly in the case that only a single complete entity were requested, the Result object returned by Session.execute() will always deliver rows (named tuples) by default; this is so that results against single or multiple ORM objects, columns, tables, etc. may all be handled identically.
If only one ORM entity was queried, the rows returned will have exactly one column, consisting of the ORM-mapped object instance for each row. To convert these rows into object instances without the tuples, the Result.scalars() method is used to first apply a “scalars” filter to the result; then the Result can be iterated or deliver rows via standard methods such as Result.all(), Result.first(), etc.
查询模型
q = select(WarrantyRequest)
returns 行,由各个模型实例组成。要改为获取原始数据行,请查询模型的 __table__
属性:
q = select(WarrantyRequest.__table__)
SQLAlchemy 的 ORM 层以面向对象的方式呈现数据库表和行,假设程序员希望使用对象及其属性而不是原始数据。