Repository: edgedb/edgedb

Issue:
run migrate create resulting bug hint
<!-- Please search existing issues to avoid creating duplicates. -->

```
C:\Users\as\hello-world-edgedb>edgedb migrate
[2022-11-25T01:36:39Z WARN  edgedb::version_check] Newer version of edgedb tool exists 2.2.6+af56767 (current 2.2.5+00d579a). To upgrade run `edgedb cli upgrade`
Connecting to an EdgeDB instance at localhost:10703...
edgedb error: InternalServerError:
  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:
      Traceback (most recent call last):
        File "/home/edgedb/.local/share/edgedb/portable/2.7/lib/python3.10/site-packages/edb/server/compiler_pool/worker_proc.py", line 55, in worker
          res = meth(*args)
        File "/home/edgedb/.local/share/edgedb/portable/2.7/lib/python3.10/site-packages/edb/server/compiler_pool/worker.py", line 185, in compile_in_tx
          units, cstate = COMPILER.compile_in_tx(cstate, *args, **kwargs)
        File "/home/edgedb/.local/share/edgedb/portable/2.7/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 2333, in compile_in_tx
          return self._compile(ctx=ctx, source=source), ctx.state
        File "/home/edgedb/.local/share/edgedb/portable/2.7/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 1807, in _compile
          return self._try_compile(ctx=ctx, source=source)
        File "/home/edgedb/.local/share/edgedb/portable/2.7/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 1871, in _try_compile
          comp, capabilities = self._compile_dispatch_ql(
        File "/home/edgedb/.local/share/edgedb/portable/2.7/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 1729, in _compile_dispatch_ql
          query = self._compile_ql_migration(
        File "/home/edgedb/.local/share/edgedb/portable/2.7/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 1055, in _compile_ql_migration
          query = self._compile_and_apply_ddl_stmt(ctx, ql)
        File "/home/edgedb/.local/share/edgedb/portable/2.7/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 951, in _compile_and_apply_ddl_stmt
          block, new_types, config_ops = self._process_delta(ctx, delta)
        File "/home/edgedb/.local/share/edgedb/portable/2.7/lib/python3.10/site-packages/edb/server/compiler/compiler.py", line 390, in _process_delta
          schema = pgdelta.apply(schema, context)
        File "/home/edgedb/.local/share/edgedb/portable/2.7/lib/python3.10/site-packages/edb/pgsql/delta.py", line 6494, in apply
          schema = super().apply(schema, context)
        File "/home/edgedb/.local/share/edgedb/portable/2.7/lib/python3.10/site-packages/edb/schema/delta.py", line 1510, in apply
          schema = self.apply_subcommands(schema, context)
        File "/home/edgedb/.local/share/edgedb/portable/2.7/lib/python3.10/site-packages/edb/pgsql/delta.py", line 147, in apply_subcommands
          schema = super().apply_subcommands(schema, context)
        File "/home/edgedb/.local/share/edgedb/portable/2.7/lib/python3.10/site-packages/edb/schema/delta.py", line 1537, in apply_subcommands
          schema = objop.apply(schema, context)
        File "/home/edgedb/.local/share/edgedb/portable/2.7/lib/python3.10/site-packages/edb/schema/delta.py", line 2996, in apply
          schema = self._create_innards(schema, context)
        File "/home/edgedb/.local/share/edgedb/portable/2.7/lib/python3.10/site-packages/edb/schema/delta.py", line 2964, in _create_innards
          return self.apply_subcommands(schema, context)
        File "/home/edgedb/.local/share/edgedb/portable/2.7/lib/python3.10/site-packages/edb/pgsql/delta.py", line 147, in apply_subcommands
          schema = super().apply_subcommands(schema, context)
        File "/home/edgedb/.local/share/edgedb/portable/2.7/lib/python3.10/site-packages/edb/schema/delta.py", line 946, in apply_subcommands
          schema = op.apply(schema, context=context)
        File "/home/edgedb/.local/share/edgedb/portable/2.7/lib/python3.10/site-packages/edb/pgsql/delta.py", line 3341, in apply
          self.apply_scheduled_inhview_updates(schema, context)
        File "/home/edgedb/.local/share/edgedb/portable/2.7/lib/python3.10/site-packages/edb/pgsql/delta.py", line 3086, in apply_scheduled_inhview_updates
          self.recreate_inhview(
        File "/home/edgedb/.local/share/edgedb/portable/2.7/lib/python3.10/site-packages/edb/pgsql/delta.py", line 3053, in recreate_inhview
          self.create_inhview(
        File "/home/edgedb/.local/share/edgedb/portable/2.7/lib/python3.10/site-packages/edb/pgsql/delta.py", line 2999, in create_inhview
          assert has_table(obj, schema)
      AssertionError
```

- EdgeDB Version:2.7
- EdgeDB CLI Version: 2.2.5+00d579a
- OS Version: windows 10 with WSL

Steps to Reproduce:

1. run `edgedb migrate` from old schema
2.

<!-- If the issue is about a query error, please also provide your schema -->

Schema:
```
module default {
    type Product {
        property sku -> str;
        multi link names := .<product[is MultiLanguage];
    }

    type MultiLanguage {
        link product -> Product;
        required property lanCode -> LanguageCode;
        required property languageText -> str;
        constraint exclusive on ((.product, .lanCode));
    }

    scalar type LanguageCode extending enum<en_US, zh_CN, fr_FR, zh_TW, da_DK>;
}
```

Here is content of `00005.edgeql`

```
CREATE MIGRATION m1l4blcvfgdb4esqln5eoj5ldlia3fggea37wpql3untg2nuelllca
    ONTO m1crkeakntw36dqzeyynk4tw4oyozr3rxaxoqppqhtqee3jvsoxkwa
{
  ALTER TYPE default::MultiLanguage {
      ALTER LINK product {
          RESET EXPRESSION;
          RESET EXPRESSION;
          RESET CARDINALITY USING (SELECT
              default::Product 
          LIMIT
              1
          );
          RESET OPTIONALITY;
          SET TYPE default::Product;
      };
  };
  ALTER TYPE default::MultiLanguage {
      CREATE CONSTRAINT std::exclusive ON ((.product, .lanCode));
  };
  ALTER TYPE default::MultiLanguage {
      DROP CONSTRAINT std::exclusive ON (.lanCode);
  };
  ALTER TYPE default::Product {
      ALTER LINK names {
          USING (.<product[IS default::MultiLanguage]);
      };
  };
};
```
dbschema folder for your reference
[dbschema.zip](https://github.com/edgedb/edgedb/files/10087999/dbschema.zip)



Discussion:
Roger, I can reproduce. 

I think that for now you can workaround it by doing a migration where you don't change the cardinality (where you make the new concrete link `multi`) and then follow up after that by making it `single`

Resolving patch:
diff --git a/edb/pgsql/delta.py b/edb/pgsql/delta.py
--- a/edb/pgsql/delta.py
+++ b/edb/pgsql/delta.py
@@ -3083,8 +3083,9 @@ def apply_scheduled_inhview_updates(
                 k for k, _ in self.inhview_updates if k not in to_recreate}
 
             for s in to_recreate:
-                self.recreate_inhview(
-                    schema, context, s, alter_ancestors=False)
+                if has_table(s, schema):
+                    self.recreate_inhview(
+                        schema, context, s, alter_ancestors=False)
 
             for s in to_alter:
                 if has_table(s, 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
@@ -13315,6 +13315,27 @@ async def test_edgeql_ddl_adjust_computed_10(self):
             ]
         )
 
+    async def test_edgeql_ddl_adjust_computed_11(self):
+        await self.con.execute(r'''
+            CREATE TYPE default::Foo;
+            CREATE TYPE default::Bar {
+                CREATE LINK foos := (default::Foo);
+            };
+        ''')
+
+        # it's annoying that we need the using on the RESET CARDINALITY;
+        # maybe we should be able to know it isn't needed
+        await self.con.execute(r'''
+            ALTER TYPE default::Bar {
+                ALTER LINK foos {
+                    RESET EXPRESSION;
+                    RESET CARDINALITY using (<Foo>{});
+                    RESET OPTIONALITY;
+                    SET TYPE default::Foo;
+                };
+            }
+        ''')
+
     async def test_edgeql_ddl_captured_as_migration_01(self):
 
         await self.con.execute(r"""