Commits

Mike Bayer committed e74627f

A :func:`.select` that is made to refer to itself in its FROM clause,
typically via in-place mutation, will raise an informative error
message rather than causing a recursion overflow.
[ticket:2815]

  • Participants
  • Parent commits 9df4651

Comments (0)

Files changed (3)

File doc/build/changelog/changelog_08.rst

     :version: 0.8.3
 
     .. change::
+        :tags: bug, sql
+        :tickets: 2815
+        :versions: 0.9.0
+
+        A :func:`.select` that is made to refer to itself in its FROM clause,
+        typically via in-place mutation, will raise an informative error
+        message rather than causing a recursion overflow.
+
+    .. change::
         :tags: bug, orm
         :tickets: 2813
         :versions: 0.9.0

File lib/sqlalchemy/sql/selectable.py

 
         def add(items):
             for item in items:
+                if item is self:
+                    raise exc.InvalidRequestError(
+                            "select() construct refers to itself as a FROM")
                 if translate and item in translate:
                     item = translate[item]
                 if not seen.intersection(item._cloned_set):

File test/sql/test_selectable.py

             "SELECT c FROM (SELECT (SELECT (SELECT table1.col1 AS a FROM table1) AS b) AS c)"
         )
 
+    def test_self_referential_select_raises(self):
+        t = table('t', column('x'))
+
+        s = select([t])
+
+        s.append_whereclause(s.c.x > 5)
+        assert_raises_message(
+            exc.InvalidRequestError,
+            r"select\(\) construct refers to itself as a FROM",
+            s.compile
+        )
+
     def test_unusual_column_elements_text(self):
         """test that .c excludes text()."""