[英]How do you add a custom column to a query in sqlalchemy if you are using the classical mapping?
一些警告:
將計算列添加到查詢中(使其包含在返回的對象中,而不是作為返回的元組中的單獨條目)的正確方法是什么?
db_query = session.query(Listing)
db_query = db_query.add_column("1 as listing_test")
print(self.db_query)
## this looks good.
# SELECT listing.id AS listing_id, 1 as listing_test
# FROM listing
# WHERE listing.id = :id_1
self.db_query.all()
## this looks bad.
# [(<flask_sqlalchemy.Lease object at 0x7fca868ff1d0>, 1)]
self.db_query.column_descriptions
[{'aliased': False,
'entity': <class 'flask_sqlalchemy.Lease'>,
'expr': <class 'flask_sqlalchemy.Lease'>,
'name': 'Lease',
'type': <class 'flask_sqlalchemy.Lease'>},
{'aliased': False,
'entity': None,
'expr': '1 as listing_test',
'name': '1 as listing_test',
'type': NullType()}]
理想情況下,listing_test只是當前返回的元組的一部分,而不是當前返回的元組。
@univerio讓我很親密!
我必須將其設置為列屬性,然后一切都很好。
class Listing(db.model):
custom_function = column_property(func.custom_function(id))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.