Repository: edgedb/edgedb

Issue:
Can't delete objects via REPL: InternalServerError: relation "..." does not exists
<!-- Please search existing issues to avoid creating duplicates. -->
- EdgeDB Version: EdgeDB CLI 2.0.4+a7fc49b, server is 1.4 Docker image on GCP
- OS Version: masOS Monterey 12.4


I added an endpoint to my new FastAPI application - one which connects to an EdgeDB instance previously accessed from my JavaScript app. It simply collects form data and creates a new `Merchant`, but as you can see by the below I'm unable to delete the created `Merchant`s from the REPL after.

```
dev> select Merchant { name, merchant_id, logo_url, emails, wallets, locations, website, business_types, main_contact_index } filter .name = "Michael";
{
  default::Merchant {
    name: 'Michael',
    merchant_id: '34550715062af006ac4fab288de67ecb44793c3a05c475227241535f6ef7a81b',
    logo_url: '',
    emails: ['test@test.com'],
    wallets: ['2umBR0hVvii7XbwL6RYmwJfmvj9dpv5LNE3hDYHqQ7LMjVxJZi0bkJ7CqXkuDKeiKWz2K7flpBLi01ln'],
    locations: ['Here, TH'],
    website: {},
    business_types: ['{business_type}'],
    main_contact_index: 0,
  },
}
dev> delete Merchant filter .name = "Michael";
edgedb error: InternalServerError: relation "edgedbpub.e63d2cdc-1e65-11ed-8843-43d0592a625a_t" 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: relation "edgedbpub.e63d2cdc-1e65-11ed-8843-43d0592a625a_t" does not exist
dev> delete Merchant filter .merchant_id = "34550715062af006ac4fab288de67ecb44793c3a05c475227241535f6ef7a81b";
edgedb error: InternalServerError: relation "edgedbpub.e63d2cdc-1e65-11ed-8843-43d0592a625a_t" 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: relation "edgedbpub.e63d2cdc-1e65-11ed-8843-43d0592a625a_t" does not exist
```


Steps to Reproduce:

1. I simply added a new endpoint to my application to create a Merchant object, e.g. `insert Merchant { name := "Michael", merchant_id := "...", ... }`
2. Attempt to delete the merchant based on either the `name` or `merchant_id` fields (secondary, primary key analogs respectively).

<!-- If the issue is about a query error, please also provide your schema -->
Schema:
[dbschema.zip](https://github.com/edgedb/edgedb/files/9501009/dbschema.zip)



Discussion:
This bug can be reproduced with the schema:
```
module default {                                                         
    type M;                                                              
    abstract type Base { link l -> M };                                  
    type A extending Base;                                               
    type B extending Base;                                               
    type L { link l -> A | B };                                          
} 
```
and doing `insert M; delete M;`

The problem is that we are including the type `A | B` in the triggers!

Resolving patch:
diff --git a/edb/pgsql/delta.py b/edb/pgsql/delta.py
--- a/edb/pgsql/delta.py
+++ b/edb/pgsql/delta.py
@@ -5807,7 +5807,7 @@ def apply(
                 if target_is_affected and current_target is not None:
                     affected_targets.add(current_target)
             else:
-                if source.is_view(eff_schema):
+                if not source.is_material_object_type(eff_schema):
                     continue
 
                 current_source = schema.get_by_id(source.id, None)
@@ -5913,7 +5913,7 @@ def apply(
                     continue
 
                 source = link.get_source(schema)
-                if source.is_view(schema):
+                if not source.is_material_object_type(schema):
                     continue
                 ptr_stor_info = types.get_pointer_storage_info(
                     link, schema=schema)


Test patch:
diff --git a/tests/test_edgeql_ddl.py b/tests/test_edgeql_ddl.py
--- a/tests/test_edgeql_ddl.py
+++ b/tests/test_edgeql_ddl.py
@@ -14022,6 +14022,28 @@ async def test_edgeql_ddl_new_required_multi_pointer_04(self):
                 ALTER TYPE Foo ALTER LINK link SET REQUIRED;
             """)
 
+    async def test_edgeql_ddl_link_union_delete_01(self):
+        await self.con.execute(r"""
+            CREATE TYPE default::M;
+            CREATE ABSTRACT TYPE default::Base {
+                CREATE LINK l -> default::M;
+            };
+            CREATE TYPE default::A EXTENDING default::Base;
+            CREATE TYPE default::B EXTENDING default::Base;
+            CREATE TYPE default::L {
+                CREATE LINK l -> (default::B | default::A);
+            };
+            CREATE TYPE ForceRedo {
+                CREATE LINK l -> default::M;
+            };
+        """)
+        await self.con.execute(r"""
+            insert M;
+        """)
+        await self.con.execute(r"""
+            delete M;
+        """)
+
     async def test_edgeql_ddl_alter_union_01(self):
         await self.con.execute(r"""
             CREATE TYPE Foo;