Skip to content Skip to sidebar Skip to footer

Why Does SQLAlchemy Label Columns In Query

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. For example, if I run the

Solution 1:

Query.statement:

The full SELECT statement represented by this Query.

The statement by default will not have disambiguating labels applied to the construct unless with_labels(True) is called first.

Using this model:

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) shows:

SELECT defaultlog.id, defaultlog.msg, defaultlog.logger_time, defaultlog.logger_line
FROM defaultlog

print(session.query(DefaultLog).with_labels().statement) shows:

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

You asked:

Why does it use an alias = original name?

From Query.with_labels docs:

...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?

Also from the Query.with_labels docs:

When the Query actually issues SQL to load rows, it always uses column labeling.

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)

... 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)

... but that takes the ORM out of the equation.


Solution 2:

It is possible to hack sqlachemy Query class to not add labels. But one must be aware that this will breaks when a table is used twice in the query. For example, self join or join thought another table.

from sqlalchemy.orm import Query

class MyQuery(Query):
    def __iter__(self):
        """Patch to disable auto labels"""
        context = self._compile_context(labels=False)
        context.statement.use_labels = False
        if self._autoflush and not self._populate_existing:
            self.session._autoflush()
        return self._execute_and_instances(context)

And then use it according to mtth answer

sessionmaker(bind=engine, query_cls=MyQuery)

Post a Comment for "Why Does SQLAlchemy Label Columns In Query"