"""
- _resolve_label = None
- """The name that should be used to identify this ColumnElement in a
- select() object when "label resolution" logic is used; this refers
- to using a string name in an expression like order_by() or group_by()
- that wishes to target a labeled expression in the columns clause.
+ _allow_label_resolve = True
+ """A flag that can be flipped to prevent a column from being resolvable
+ by string label name.
- The name is distinct from that of .name or ._label to account for the case
- where anonymizing logic may be used to change the name that's actually
- rendered at compile time; this attribute should hold onto the original
- name that was user-assigned when producing a .label() construct.
+ The joined eager loader strategy in the ORM uses this, for example.
"""
- _allow_label_resolve = True
- """A flag that can be flipped to prevent a column from being resolvable
- by string label name."""
-
_is_implicitly_boolean = False
_alt_names = ()
# help in those cases where text() is
# interpreted in a column expression situation
- key = _label = _resolve_label = None
+ key = _label = None
_allow_label_resolve = False
if name:
self.name = name
-
- # TODO: nothing fails if this is removed. this is related
- # to the order_by() string feature tested in test_text.py.
- self._resolve_label = self.name
else:
self.name = _anonymous_label.safe_construct(
id(self), getattr(element, "name", "anon")
self._reset_memoizations()
self._element = clone(self._element, **kw)
if anonymize_labels:
- self.name = self._resolve_label = _anonymous_label.safe_construct(
+ self.name = _anonymous_label.safe_construct(
id(self), getattr(self.element, "name", "anon")
)
self.key = self._tq_label = self._tq_key_label = self.name
"""part of #2992; make sure string label references can't
access an eager loader, else an eager load can corrupt the query.
+ This behavior relies upon the allow_label_resolve flag to disable
+ a column expression from being resolvable in an "order by label"
+ context.
+
"""
Address, addresses, users, User = (
self.classes.Address,
from sqlalchemy.testing import AssertsCompiledSQL
from sqlalchemy.testing import eq_
from sqlalchemy.testing import fixtures
+from sqlalchemy.testing.assertions import expect_raises_message
from sqlalchemy.types import NullType
table1 = table(
stmt, "SELECT mytable.myid AS foo FROM mytable ORDER BY foo"
)
+ def test_no_order_by_text(self):
+ stmt = select(text("foo")).order_by("foo")
+
+ with expect_raises_message(
+ exc.CompileError,
+ r"Can't resolve label reference for ORDER BY / GROUP BY / ",
+ ):
+ stmt.compile()
+
def test_order_by_colname(self):
stmt = select(table1.c.myid).order_by("name")
self.assert_compile(