# HG changeset patch
# User Sylvain Thénault <sylvain.thenault@logilab.fr>
# Date 1284014534 -7200
#      Thu Sep 09 08:42:14 2010 +0200
# Branch stable
# Node ID 341c6980df52fff41cfc0519473b417ab9858e83
# Parent  2e878b60d58108c43a838b4a27323ea93558edc8
fix subquery_selection_index responsability mess-up

it wasn't doing what it should have done (see cw.rset related_entity
implementation)

diff --git a/ChangeLog b/ChangeLog
--- a/ChangeLog
+++ b/ChangeLog
@@ -8,6 +8,9 @@
     * add constraint package to dependencies  so we've fallback opportunity if
       gecode is not installed
 
+    * fix subquery_selection_index responsability mess-up: it wasn't doing what
+      it should have done (see cw.rset related_entity implementation)
+
 2010-08-02  --  0.26.5
     * fix solutions computation crash with some query using sub-queries (closes #37423)
 
diff --git a/stmts.py b/stmts.py
--- a/stmts.py
+++ b/stmts.py
@@ -327,14 +327,16 @@
         return self._subq_cache[(col, etype)]
 
     def subquery_selection_index(self, subselect, col):
-        """given a select sub-query and a column index in this sub-query, return
-        the selection index for this column in the root query
+        """given a select sub-query and a column index in the root query, return
+        the selection index for this column in the sub-query
         """
-        while col is not None and subselect.parent.parent:
+        selectpath = []
+        while subselect.parent.parent is not None:
             subq = subselect.parent.parent
             subselect = subq.parent
-            termvar = subselect.aliases[subq.aliases[col].name]
-            col = termvar.selected_index()
+            selectpath.insert(0, subselect)
+        for select in selectpath:
+            col = select.selection[col].variable.colnum
         return col
 
     # recoverable modification methods ########################################