为什么 SQLAlchemy 在查询中标记列 [英] Why Does SQLAlchemy Label Columns in Query
问题描述
当我在 SQLAlchemy 中进行查询时,我注意到查询对每一列使用 AS 关键字.它为每一列设置 alias_name
= column_name
.
When I make a query in SQLAlchemy, I noticed that the queries use the AS keyword for each column. It sets the alias_name
= column_name
for every column.
例如,如果我运行命令 print(session.query(DefaultLog))
,它会返回:
For example, if I run the command print(session.query(DefaultLog))
, it returns:
注意:DefaultLog
是我的表对象.
Note: DefaultLog
is my table object.
SELECT default_log.id AS default_log_id, default_log.msg AS default_log_msg, default_log.logger_time AS default_log_logger_time, default_log.logger_line AS default_log_logger_line, default_log.logger_filepath AS default_log_logger_filepath, default_log.level AS default_log_level, default_log.logger_name AS default_log_logger_name, default_log.logger_method AS default_log_logger_method, default_log.hostname AS default_log_hostname
FROM default_log
为什么它使用别名=原始名称?有什么方法可以禁用这种行为吗?
Why does it use an alias = original name? Is there some way I can disable this behavior?
先谢谢你!
推荐答案
此查询表示的完整 SELECT 语句.
The full SELECT statement represented by this Query.
该语句默认不会应用消歧标签除非首先调用 with_labels(True) 到构造.
The statement by default will not have disambiguating labels applied to the construct unless with_labels(True) is called first.
使用此模型:
class DefaultLog(Base):
id = sa.Column(sa.Integer, primary_key=True)
msg = sa.Column(sa.String(128))
logger_time = sa.Column(sa.DateTime)
logger_line = sa.Column(sa.Integer)
print(session.query(DefaultLog).statement)
显示:
SELECT defaultlog.id, defaultlog.msg, defaultlog.logger_time, defaultlog.logger_line
FROM defaultlog
print(session.query(DefaultLog).with_labels().statement)
显示:
SELECT defaultlog.id AS defaultlog_id, defaultlog.msg AS defaultlog_msg, defaultlog.logger_time AS defaultlog_logger_time, defaultlog.logger_line AS defaultlog_logger_line
FROM defaultlog
你问:
为什么它使用别名 = 原始名称?
Why does it use an alias = original name?
来自查询.with_labels
文档:
...这通常用于消除多个表中具有相同名称的列的歧义.
...this is commonly used to disambiguate columns from multiple tables which have the same name.
因此,如果您想发出调用多个表的单个查询,则无法阻止那些具有共享相同名称的列的表.
So if you want to issue a single query that calls upon multiple tables, there is nothing stopping those tables having columns that share the same name.
有什么方法可以禁用这种行为吗?
Is there some way I can disable this behavior?
同样来自 Query.with_labels
文档:
当Query实际发出SQL加载行时,总是使用列标签.
When the Query actually issues SQL to load rows, it always uses column labeling.
所有检索行的方法(get()
、one()
、one_or_none()
、all()
并通过 Query.__iter__()
方法:
All of the methods that retrieve rows (get()
, one()
, one_or_none()
, all()
and iterating over the Query
) route through the Query.__iter__()
method:
def __iter__(self):
context = self._compile_context()
context.statement.use_labels = True
if self._autoflush and not self._populate_existing:
self.session._autoflush()
return self._execute_and_instances(context)
... 这行硬编码标签用法:context.statement.use_labels = True
.所以它是烘焙"的,不能被禁用.
... where this line hard codes the label usage: context.statement.use_labels = True
. So it is "baked in" and can't be disabled.
你可以执行没有标签的语句:
You can execute the statement without labels:
session.execute(session.query(DefaultLog).statement)
...但这将 ORM 排除在外.
... but that takes the ORM out of the equation.
这篇关于为什么 SQLAlchemy 在查询中标记列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!