Repository: edgedb/edgedb

Issue:
Bare backlinks causing problems in queries
<!-- Please search existing issues to avoid creating duplicates. -->
- EdgeDB Version: EdgeDB 1.0-beta.3+dev5677.d2021061821.g63abb394e.cv202105190000
- OS Version: linux

Steps to Reproduce:

```
edgedb> insert User;
{default::User {id: 4675d496-d098-11eb-82da-5f571df8c8d3}}
edgedb> insert Post{user := (select User limit 1)};
{default::Post {id: 4e0a6a32-d098-11eb-82da-3bc279c6aa72}}
edgedb> select User{posts:{id}};
{default::User {posts: {default::Post {id: 4e0a6a32-d098-11eb-82da-3bc279c6aa72}}}}
edgedb> \set limit 0
edgedb> select User{posts:{id}};
ERROR: InternalServerError: column BaseObject~1.6fdec65c-d08d-11eb-abf3-41362b4f4947 does not exist
  Hint: This is most likely a bug in EdgeDB. Please consider opening an issue ticket at https://github.com/edgedb/edgedb/issues/new?template=bug_report.md
  Server traceback:
      edb.errors.InternalServerError: column BaseObject~1.6fdec65c-d08d-11eb-abf3-41362b4f4947 does not exist
```
<!-- If the issue is about a query error, please also provide your schema -->
Schema:
```
type User {
    link posts := .<user;
}   

abstract type Action {
    required link user -> User;
}

type Post extending Action;
```

This is what happens in the `test_edgeql_migration_backlink_01` in https://github.com/edgedb/edgedb/pull/2604


Discussion:

Resolving patch:
diff --git a/edb/edgeql/compiler/viewgen.py b/edb/edgeql/compiler/viewgen.py
--- a/edb/edgeql/compiler/viewgen.py
+++ b/edb/edgeql/compiler/viewgen.py
@@ -490,6 +490,17 @@ def _normalize_view_ptr_expr(
             name=ptrcls.get_shortname(ctx.env.schema).name,
         )
 
+        # Schema computables that point to opaque unions will just have
+        # BaseObject as their target, but in order to properly compile
+        # it, we need to know the actual type here, so we recompute it.
+        # XXX: This is a hack, though, and hopefully we can fix it once
+        # the computable/alias rework lands.
+        is_opaque_schema_computable = (
+            ptrcls.is_pure_computable(ctx.env.schema)
+            and (t := ptrcls.get_target(ctx.env.schema))
+            and t.get_name(ctx.env.schema) == sn.QualName('std', 'BaseObject')
+        )
+
         base_required = base_ptrcls.get_required(ctx.env.schema)
         base_cardinality = _get_base_ptr_cardinality(base_ptrcls, ctx=ctx)
         base_is_singleton = False
@@ -504,6 +515,7 @@ def _normalize_view_ptr_expr(
             or is_polymorphic
             or target_typexpr is not None
             or (ctx.implicit_limit and not base_is_singleton)
+            or is_opaque_schema_computable
         ):
 
             if target_typexpr is None:


Test patch:
diff --git a/tests/test_edgeql_select.py b/tests/test_edgeql_select.py
--- a/tests/test_edgeql_select.py
+++ b/tests/test_edgeql_select.py
@@ -6162,3 +6162,23 @@ async def test_edgeql_select_reverse_overload_02(self):
             ''',
             [{"z": [{"name": "Regression."}, {"name": "Release EdgeDB"}]}],
         )
+
+    async def test_edgeql_select_bare_backlink_01(self):
+        await self.con.execute('''
+            CREATE ABSTRACT TYPE Action;
+            CREATE TYPE Post EXTENDING Action;
+            CREATE TYPE Thing;
+            ALTER TYPE Action {
+                CREATE REQUIRED LINK thing -> Thing;
+            };
+            ALTER TYPE Thing {
+                CREATE LINK posts := (.<thing);
+            };
+        ''')
+
+        await self.assert_query_result(
+            r'''
+                 SELECT Thing { posts: {id} };
+            ''',
+            [],
+        )