Repository: edgedb/edgedb

Issue:
InternalServerError: relation "edgedbpub.f4565ed4-d599-11ed-8af8-c1d4769f57f1" does not exist
<!-- Please search existing issues to avoid creating duplicates. -->
I did search and found nothing related to my specific problem. 

<!--
For the EdgeDB Version: run `edgedb query 'select sys::get_version_as_str()'` from your project directory (or run `select sys::get_version_as_str();` in the EdgeDB interactive shell).
For the EdgeDB CLI Version: Run `edgedb --version` from anywhere
-->

- EdgeDB Version: "2.13+ad0eb0f"
- EdgeDB CLI Version: EdgeDB CLI 2.3.1+5d93f42
- OS Version: OSX Monterey 12.6

Steps to Reproduce:

I'm not completely sure because I was just playing around with this tech for the first time.

**The migration I am trying to run is:**
```edgeql
~/edgedb-tryout/dbschema/migrations   
❯ cat 00011.edgeql 
CREATE MIGRATION m1cmmwm622tqf33h4of4onkwxmlnhld4ytjlja2tcq5lfunjpyjcyq
    ONTO m12jnyq2f35weouxers2fn2b4fll4tf4tbu76pf4s7bwajg5ptiacq
{
  ALTER TYPE default::HelpRequest {
      CREATE REQUIRED PROPERTY content -> std::str {
          SET REQUIRED USING ('Test');
      };
  };
  ALTER TYPE default::Response {
      CREATE REQUIRED PROPERTY content -> std::str {
          SET REQUIRED USING ('Comment');
      };
  };
};
```

**Schema**
```
~/edgedb-tryout   
❯ edgedb describe schema 
using future nonrecursive_access_policies;
module default {
    type Account {
        multi link help_requests := (.<account[is default::HelpRequest]);
    };
    type HelpRequest {
        required link account -> default::Account;
        multi link responses_to_help_request := (.<response_to[is default::Response]);
        required property created_at -> std::datetime {
            default := (std::datetime_current());
        };
    };
    type Response {
        required link response_to -> (default::Response | default::HelpRequest);
        multi link responses_to_response := (.<response_to[is default::Response]);


        required property created_at -> std::datetime {
            default := (std::datetime_current());
        };
    };
};
```

**All migrations**
[migrations.zip](https://github.com/edgedb/edgedb/files/11183217/migrations.zip)


Discussion:

Resolving patch:
diff --git a/edb/pgsql/delta.py b/edb/pgsql/delta.py
--- a/edb/pgsql/delta.py
+++ b/edb/pgsql/delta.py
@@ -5124,11 +5124,10 @@ def _alter_begin(
     ) -> s_schema.Schema:
         orig_schema = schema
         schema = super()._alter_begin(schema, context)
-        pop = self.get_parent_op(context)
         orig_type = self.scls.get_target(orig_schema)
         new_type = self.scls.get_target(schema)
         if (
-            not pop.maybe_get_object_aux_data('from_alias')
+            has_table(self.scls.get_source(schema), schema)
             and not self.scls.is_pure_computable(schema)
             and (orig_type != new_type or self.cast_expr is not None)
         ):
@@ -5148,7 +5147,6 @@ def _alter_innards(
         context: sd.CommandContext,
     ) -> s_schema.Schema:
         orig_schema = context.current().original_schema
-        pop = self.get_parent_op(context)
 
         # We need to run the parent change *before* the children,
         # or else the view update in the child might fail if a
@@ -5156,7 +5154,7 @@ def _alter_innards(
         if (
             not self.scls.generic(schema)
             and not self.scls.is_pure_computable(schema)
-            and not pop.maybe_get_object_aux_data('from_alias')
+            and has_table(self.scls.get_source(schema), schema)
         ):
             orig_card = self.scls.get_cardinality(orig_schema)
             new_card = self.scls.get_cardinality(schema)
@@ -5175,7 +5173,6 @@ def apply(
         schema: s_schema.Schema,
         context: sd.CommandContext,
     ) -> s_schema.Schema:
-        pop = self.get_parent_op(context)
         orig_schema = schema
         schema = super().apply(schema, context)
 
@@ -5183,7 +5180,7 @@ def apply(
             orig_required = self.scls.get_required(orig_schema)
             new_required = self.scls.get_required(schema)
             if (
-                not pop.maybe_get_object_aux_data('from_alias')
+                has_table(self.scls.get_source(schema), schema)
                 and not self.scls.is_endpoint_pointer(schema)
                 and not self.scls.is_pure_computable(schema)
                 and orig_required != new_required
@@ -5595,13 +5592,12 @@ def _alter_begin(
         schema: s_schema.Schema,
         context: sd.CommandContext,
     ) -> s_schema.Schema:
-        pop = self.get_parent_op(context)
         orig_schema = schema
         schema = super()._alter_begin(schema, context)
         orig_type = self.scls.get_target(orig_schema)
         new_type = self.scls.get_target(schema)
         if (
-            not pop.maybe_get_object_aux_data('from_alias')
+            has_table(self.scls.get_source(schema), schema)
             and not self.scls.is_pure_computable(schema)
             and not self.scls.is_endpoint_pointer(schema)
             and (orig_type != new_type or self.cast_expr is not None)
@@ -5619,7 +5615,6 @@ def _alter_innards(
         schema: s_schema.Schema,
         context: sd.CommandContext,
     ) -> s_schema.Schema:
-        pop = self.get_parent_op(context)
         orig_schema = context.current().original_schema
 
         # We need to run the parent change *before* the children,
@@ -5629,7 +5624,7 @@ def _alter_innards(
             not self.scls.generic(schema)
             and not self.scls.is_pure_computable(schema)
             and not self.scls.is_endpoint_pointer(schema)
-            and not pop.maybe_get_object_aux_data('from_alias')
+            and has_table(self.scls.get_source(schema), schema)
         ):
             orig_card = self.scls.get_cardinality(orig_schema)
             new_card = self.scls.get_cardinality(schema)
@@ -5648,7 +5643,6 @@ def apply(
         schema: s_schema.Schema,
         context: sd.CommandContext,
     ) -> s_schema.Schema:
-        pop = self.get_parent_op(context)
         orig_schema = schema
         schema = super().apply(schema, context)
 
@@ -5656,7 +5650,7 @@ def apply(
             orig_required = self.scls.get_required(orig_schema)
             new_required = self.scls.get_required(schema)
             if (
-                not pop.maybe_get_object_aux_data('from_alias')
+                has_table(self.scls.get_source(schema), schema)
                 and not self.scls.is_endpoint_pointer(schema)
                 and not self.scls.is_pure_computable(schema)
                 and orig_required != new_required


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
@@ -15404,6 +15404,22 @@ async def test_edgeql_ddl_computed_intersection_lprop_01(self):
             select ObjectType {links: {@owned}};
         """)
 
+    async def test_edgeql_ddl_union_link_target_alter_01(self):
+        await self.con.execute(r"""
+            create type X;
+            create type Y;
+            create type T { create link xy -> X | Y; };
+        """)
+        await self.con.query(r"""
+            alter type X create required property foo -> str;
+        """)
+        await self.con.query(r"""
+            alter type Y create required property foo -> str;
+        """)
+        await self.con.query(r"""
+            alter type X alter property foo set multi;
+        """)
+
     async def test_edgeql_ddl_rebase_views_01(self):
         await self.con.execute(r"""
             CREATE TYPE default::Foo {