Repository: edgedb/edgedb

Issue:
InternalServerError: relation "edgedbpub.083d33d2-5c14-11ed-9c26-ff2a5a49c33f_t" does not exist
<!-- Please search existing issues to avoid creating duplicates. -->
- EdgeDB Version: EdgeDB CLI 2.1.0+527e309
- OS Version: MacOS 12.6 (21G115)

The error message in the title comes up when I try to delete a type in the default module.
I guess this is about the current state of the database.
Can I export and upload it here somehow?
(There is not very much data in the database yet.)

Steps to Reproduce:

1. drop type MyType




Discussion:
This is a dump created with `edgedb dump` in my project directory.
I hope it helps.

[error_dropping_types.edgedb.zip](https://github.com/edgedb/edgedb/files/9962491/error_dropping_types.edgedb.zip)

The database in the dump you gave me doesn't seem to have a type `MyType` (or any type in the default module). Did you expect it to?

What would be most useful is the series of migrations you ran to get to the state where this happened, though a dump that can reproduce it would be fine too.

Also, you gave your CLI version; which version of the server was in use?
Hi @msullivan, thanks for looking into this!
> The database in the dump you gave me doesn't seem to have a type MyType (or any type in the default module). Did you expect it to?

Yes, the type is shown in the edgedb ui. I realised now however, that there is a second database `edgedb`, which I did not use. I have created a new dump with `--all` and the `varycon.dump` should contain the database showing the error.

[dump.zip](https://github.com/edgedb/edgedb/files/9969029/dump.zip)

> What would be most useful is the series of migrations you ran to get to the state where this happened, though a dump that can reproduce it would be fine too. 

I did use DDL rather than migrations since the application I am implementing allows creating types at run-time.

> Also, you gave your CLI version; which version of the server was in use?

I hope, this is the information you are expecting:

~~~
$ edgedb instance list
┌───────┬─────────┬───────┬─────────────┬─────────┐
│ Kind  │ Name    │ Port  │ Version     │ Status  │
├───────┼─────────┼───────┼─────────────┼─────────┤
│ local │ varycon │ 10701 │ 2.6+db69671 │ running │
└───────┴─────────┴───────┴─────────────┴─────────┘
~~~
@msullivan does my information work for you now?
Yes, thank you, I can reproduce the bug now. (And found an unfortunate extra bug that prevented the database from restoring properly... See https://github.com/edgedb/edgedb/pull/4668)

Resolving patch:
diff --git a/edb/pgsql/delta.py b/edb/pgsql/delta.py
--- a/edb/pgsql/delta.py
+++ b/edb/pgsql/delta.py
@@ -5929,7 +5929,11 @@ def apply(
                 # actually process this for each link.
                 if (
                     (action is DA.Restrict or action is DA.DeferredRestrict)
-                    and link.field_is_inherited(schema, 'on_target_delete')
+                    and (
+                        link.field_is_inherited(schema, 'on_target_delete')
+                        or link.get_explicit_field_value(
+                            schema, 'on_target_delete', None) is None
+                    )
                     and link.get_implicit_bases(schema)
                 ):
                     continue
@@ -5950,12 +5954,20 @@ def apply(
                     else:
                         links.append(link)
 
+            # The ordering that we process links matters: Restrict
+            # must be processed *after* Allow and DeleteSource,
+            # because Restrict is applied (via views) to all
+            # descendant links regardless of whether they have been
+            # overridden, and so Allow and DeleteSource must be
+            # handled first.
+            ordering = (DA.Restrict, DA.Allow, DA.DeleteSource)
+
             links.sort(
-                key=lambda l: (l.get_on_target_delete(schema),
+                key=lambda l: (ordering.index(l.get_on_target_delete(schema)),
                                l.get_name(schema)))
 
             inline_links.sort(
-                key=lambda l: (l.get_on_target_delete(schema),
+                key=lambda l: (ordering.index(l.get_on_target_delete(schema)),
                                l.get_name(schema)))
 
             deferred_links.sort(


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
@@ -13811,6 +13811,17 @@ async def test_edgeql_ddl_link_policy_16(self):
             [],
         )
 
+    async def test_edgeql_ddl_link_policy_implicit_01(self):
+        await self.con.execute("""
+            create type T;
+            create type X {
+                create link foo -> schema::ObjectType;
+            };
+        """)
+        await self.con.execute("""
+            drop type T;
+        """)
+
     async def test_edgeql_ddl_dupe_link_storage_01(self):
         await self.con.execute(r"""