社区! 我使用 MySQL 并需要将查询“翻译”为 Pony ORM。 我有一个包含很多列的大表“客户”,我的问题是在使用数据库的方法中编写每一列。所以我有类似的东西:
@db_session
def get_client_info_by(self, **criteria) -> Union[dict, None]:
"""Return client's info from database by specified criteria"""
if not criteria: # If no criteria provided, return None
return None
query = select(c for c in self.clients)
for attr, value in criteria.items(): # Add filters to the query based on the criteria
query = query.filter(lambda c: getattr(c, attr) == value)
client = query[:]
if client:
client_info = {
'id': client[0].id,
'first_name': client[0].first_name,
'last_name': client[0].last_name,
'type': client[0].type,
'language_id': client[0].language_id,
'country_id': client[0].country_id,
# a lot of other columns ... (about 50)
'created_at': client[0].created_at,
'updated_at': client[0].updated_at
}
return client_info
else:
return None
嗯,我需要查询而不写所有列,也许有像“SELECT * FROM ...”这样的查询来获取有关客户端的所有信息,但不仅仅是特定信息。
您是否尝试过原始 SQL 查询示例
clients.select_by_sql('SELECT * FROM clients')