如何从 SQLAlchemy 结果中获取列名(声明性语法)
我正在一个金字塔项目中工作,并且我在 SQLAlchemy 中以声明性语法列出了表,
"""models.py"""
class Projects(Base):
__tablename__ = 'projects'
__table_args__ = {'autoload': True}
来获取结果
""""views.py"""
session = DBSession()
row_data = session.query(Projects).filter_by(id=1).one()
我通过使用如何从该结果中获取列名
。 PS:我无法使用此方法,因为我正在使用声明性语法。
I am working in a pyramid project and I've the table in SQLAlchemy in declarative syntax
"""models.py"""
class Projects(Base):
__tablename__ = 'projects'
__table_args__ = {'autoload': True}
I get the results by using
""""views.py"""
session = DBSession()
row_data = session.query(Projects).filter_by(id=1).one()
How can I get the column names from this result.
PS: I am unable to use this method since I am using the declarative syntax.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以执行类似于 Foo Stack 的答案的操作,而无需诉诸私有字段,方法是:
You can do something similar to Foo Stack's answer without resorting to private fields by doing:
然后这将返回列名称 ['id', 'date', 'value', ...]:
或者这个
Then this will return the columns names ['id', 'date', 'value', ...]:
Or this
ORM和非ORM的区别,不是声明式的,它只是ORM的一个助手。
查询有一个为此目的添加的属性
column_descriptions
:https://docs.sqlalchemy.org/en/20/orm/queryguide/query.html#sqlalchemy.orm.Query.column_descriptions
The difference is between ORM and non-ORM, not declarative, which is just a helper for the ORM.
Query has an attribute
column_descriptions
that was added for this purpose:https://docs.sqlalchemy.org/en/20/orm/queryguide/query.html#sqlalchemy.orm.Query.column_descriptions
只是玩玩,这种语法将为您提供所有列(因此为了解决您的问题,请将查询设置为仅查看一个表/对象):
Just playing around, this syntax will give you all the columns (so to solve your problem, set query to look at one table/object only):
简短的回答是我最终得到了以下解决方案:
为什么?
我有一个类似的用例,我需要知道查询返回的显式列(即查询不一定包含表类的所有列)。由于该表的大小也很大(数百万个条目),prolibertas 答案在性能方面并不令人满意。下面是我的 94 列表的性能比较:
Short answer is that I ended up with the following solution:
Why?
I had a similar use case where I need to know the explicit columns returned by a query (i.e., the query does not necessarily contain all columns of a table class). Since the table is also massive in size (millions of entries), prolibertas answer was not satisfying in terms of performance. Here a performance comparison on my table with 94 columns:
此链接展示了如何获取您可能需要的有关表、列等的所有元数据。
SQLAlchemy 元数据
上面的许多答案都基于此页面上的信息。
假设我们声明了一个表。
以下是获取有关表的元数据的一些示例。
This link shows how to get all the metadata you could ever need about a table, column and more.
SQLAlchemy Metadata
Many of the answers above are based on the info on this page.
Suppose we have declared a table.
Here are some examples of getting metadata about the table.
就
在
示例之后:
结果:
Just
After
Example :
Result :
在 2023 年手册中建议使用以下行:
或者如果您使用“执行”语句:
In 2023 manual advise to use following lines:
or if you are using "execute" statement:
想扩展@zzzeek的答案。事实上,Query 有
column_descriptions
属性,但并非所有方法都可用。考虑以下两个查询:
因此,如果您遇到这种情况,您需要使用
column_descriptions
属性,但使用...query(...).all()
那么您可以将其更改为...query(...).filter_by()
,即filter_by()
,无需任何过滤条件。Would like to extend @zzzeek's answer. Indeed Query has
column_descriptions
attribute but it's not available for all the methods.Consider the following two queries:
So if you come across this situation where you need to use
column_descriptions
attribute but using...query(...).all()
then you can change it to...query(...).filter_by()
i.e.filter_by()
without any filter condition.