Repository: edgedb/edgedb

Issue:
ISE: Relation edgedbpub.<uuid> does not exist during introspection
On latest master branch, executing 

```
select schema::InheritingObject {
  children := .<bases[IS schema::Type],
  descendants := .<ancestors[IS schema::Type]
};
```

will trigger the above ISE.

This is a separate issue from #5358.



Discussion:
Perhaps another bug: `select schema::InheritingObject {children := .<bases[is schema::InheritingObject], descendants := .<ancestors[is schema::InheritingObject]};` is giving a `edgedb error: ProtocolEncodingError: object data size does not match its shape` error. 
Looks like the root cause of the issue is that we have the wrong derived view during back link traversal :
`edgedbpub."__derived__::(schema:ObjectType | schema:ScalarType).ancestors_t"`

as in the following SQL snippet
```
FROM (
                            (
                            (
                                FROM edgedbstd."schema::ObjectType.ancestors_t" AS "ancestors~1"
                                SELECT/*<pg.SelectStmt at 0x110ddb8b0>*/
                                    "ancestors~1".source AS source,
                                    "ancestors~1".target AS target
                            ) union ALL (
                                FROM edgedbpub."__derived__::(schema:ObjectType | schema:ScalarType).ancestors_t" AS "ancestors~2"
                                SELECT/*<pg.SelectStmt at 0x110dd9720>*/
                                    "ancestors~2".source AS source,
                                    "ancestors~2".target AS target
                            )
                            ) union ALL (
                                FROM edgedbstd."schema::ScalarType.ancestors_t" AS "ancestors~3"
                                SELECT/*<pg.SelectStmt at 0x110dd8580>*/
                                    "ancestors~3".source AS source,
                                    "ancestors~3".target AS target
                            )
                            ) AS "ancestors~4"
JOIN LATERAL ...
```

the second derived table should just be `edgedbstd."schema::ObjectType.ancestors_t"`

Resolving patch:
diff --git a/edb/schema/objtypes.py b/edb/schema/objtypes.py
--- a/edb/schema/objtypes.py
+++ b/edb/schema/objtypes.py
@@ -186,28 +186,19 @@ def getrptrs(
         if sn.is_qualified(name):
             raise ValueError(
                 'references to concrete pointers must not be qualified')
-        ptrs = {
-            lnk for lnk in schema.get_referrers(self, scls_type=links.Link,
-                                                field_name='target')
-            if (
-                lnk.get_shortname(schema).name == name
-                and not lnk.get_source_type(schema).is_view(schema)
-                # Only grab the "base" pointers
-                and all(
-                    b.generic(schema)
-                    for b in lnk.get_bases(schema).objects(schema)
-                )
-                and (not sources or lnk.get_source_type(schema) in sources)
-            )
-        }
 
-        for obj in self.get_ancestors(schema).objects(schema):
+        ptrs: Set[links.Link] = set()
+
+        ancestor_objects = self.get_ancestors(schema).objects(schema)
+
+        for obj in (self,) + ancestor_objects:
             ptrs.update(
                 lnk for lnk in schema.get_referrers(obj, scls_type=links.Link,
                                                     field_name='target')
                 if (
                     lnk.get_shortname(schema).name == name
                     and not lnk.get_source_type(schema).is_view(schema)
+                    and not lnk.get_source_type(schema).is_union_type(schema)
                     # Only grab the "base" pointers
                     and all(
                         b.generic(schema)


Test patch:
diff --git a/tests/test_edgeql_introspection.py b/tests/test_edgeql_introspection.py
--- a/tests/test_edgeql_introspection.py
+++ b/tests/test_edgeql_introspection.py
@@ -1580,3 +1580,20 @@ async def test_edgeql_introspection_cfg_objects_02(self):
             ''',
             [True],
         )
+
+    async def test_edgeql_schema_inheritance_computed_backlinks_01(self):
+        await self.assert_query_result(
+            r'''WITH MODULE schema
+            SELECT InheritingObject {
+                children := .<bases[IS Type],
+                descendants := .<ancestors[IS Type]
+            } LIMIT 2''',
+            [
+                {'children': [],
+                 'descendants': []
+                 },
+                {'children': [],
+                 'descendants': []
+                 },
+            ]
+        )