diff --git a/nodes.py b/nodes.py
index f10d26835b70f8aeaaa8c5134927c0ea3fa6f9cc_bm9kZXMucHk=..0672043cd73a6d2ee3386c1a44df63268dcad056_bm9kZXMucHk= 100644
--- a/nodes.py
+++ b/nodes.py
@@ -440,7 +440,12 @@
             return False
         rhs = self.children[1]
         if isinstance(rhs, Comparison):
-            rhs = rhs.children[0]
+            try:
+                rhs = rhs.children[0]
+            except:
+                print 'opppp', rhs
+                print rhs.root
+                raise
         # else: relation used in SET OR DELETE selection
         return ((isinstance(rhs, Constant) and rhs.type == 'etype')
                 or (isinstance(rhs, Function) and rhs.name == 'IN'))
diff --git a/stcheck.py b/stcheck.py
index f10d26835b70f8aeaaa8c5134927c0ea3fa6f9cc_c3RjaGVjay5weQ==..0672043cd73a6d2ee3386c1a44df63268dcad056_c3RjaGVjay5weQ== 100644
--- a/stcheck.py
+++ b/stcheck.py
@@ -27,7 +27,7 @@
 from rql._exceptions import BadRQLQuery
 from rql.utils import function_description
 from rql.nodes import (Relation, VariableRef, Constant, Not, Exists, Function,
-                       Variable, variable_refs)
+                       And, Variable, variable_refs, make_relation)
 from rql.stmts import Union
 
 
@@ -517,7 +517,7 @@
                 for vref in term.get_nodes(VariableRef):
                     bloc_simplification(vref.variable, term)
 
-    def rewrite_shared_optional(self, exists, var):
+    def rewrite_shared_optional(self, exists, var, identity_rel_scope=None):
         """if variable is shared across multiple scopes, need some tree
         rewriting
         """
@@ -574,5 +574,10 @@
             newvar.stinfo['possibletypes'] = var.stinfo['possibletypes']
             for sol in newvar.stmt.solutions:
                 sol[newvar.name] = sol[var.name]
-        rel = exists.add_relation(var, 'identity', newvar)
+        if identity_rel_scope is None:
+            rel = exists.add_relation(var, 'identity', newvar)
+            identity_rel_scope = exists
+        else:
+            rel = make_relation(var, 'identity', (newvar,), VariableRef)
+            exists.parent.replace(exists, And(exists, Exists(rel)))
         # we have to force visit of the introduced relation
@@ -578,5 +583,5 @@
         # we have to force visit of the introduced relation
-        self.visit_relation(rel, exists)
+        self.visit_relation(rel, identity_rel_scope)
         return newvar
 
     # tree nodes ##############################################################