Repository: edgedb/edgedb

Issue:
ISE: relation does not exist
<!-- Please search existing issues to avoid creating duplicates. -->

<!--
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: 3.4+6d5921b
- EdgeDB CLI Version:  3.5.0+e1ad387
- OS Version: Mac 13.4

Steps to Reproduce:

1. Made this schema change
	 ```diff
	module default {
	  abstract type Resource {
	    required modifiedAt: datetime {
	-    rewrite insert, update using (datetime_of_statement());
	+    default := datetime_of_statement();
	+    rewrite update using (datetime_of_statement());
	    };
	  };
	  type User extending Resource {
	    email: str {
	      constraint exclusive;
	    };
	  }
	  alias RootUser := (
	    SELECT User
	    FILTER .email = 'my-root-admin@email.com'
	  );
	}
	```
3. Which created this migration
	```
	CREATE MIGRATION m1n6uximg2ikszlalug3cmjggh5lss4ub3nnwyuzb5iiuadzemksaa
	    ONTO m1a2sjawhkocczaahomg5inph7oitcdwjmxnkb5km774gb75c4ixya
	{
	  ALTER TYPE default::Resource {
	      ALTER PROPERTY modifiedAt {
	          SET default := (std::datetime_of_statement());
	          DROP REWRITE
	              INSERT ;
	          };
	      };
	};
	```
4.
	```
	$ edgedb migrate
	edgedb error: InternalServerError: relation "edgedbpub.f960cdbe-6771-11ee-bad9-75c4ad4d518c" does not exist
	```



Discussion:
I was unable to reproduce this issue using the information provided. Did you run into this problem with exactly this schema?
No this was a subset, but only by a little bit. I had a pre-existing `type User extending Resource` as well, maybe that? I can test locally some more, since you are unable to reproduce.
Ok I updated the OP @msullivan. The `RootUser` alias was required to reproduce. Maybe this is a bad practice? 😬 
Roger, reproduced!
And, unfortunately, this looks like it was a regression in 3.4 introduced by #6134.

Resolving patch:
diff --git a/edb/pgsql/compiler/stmt.py b/edb/pgsql/compiler/stmt.py
--- a/edb/pgsql/compiler/stmt.py
+++ b/edb/pgsql/compiler/stmt.py
@@ -21,6 +21,8 @@
 
 from typing import *
 
+from edb import errors
+
 from edb.ir import ast as irast
 from edb.ir import utils as irutils
 
@@ -41,6 +43,10 @@ def compile_SelectStmt(
         ctx: context.CompilerContextLevel) -> pgast.BaseExpr:
 
     if ctx.singleton_mode:
+        if not irutils.is_trivial_select(stmt):
+            raise errors.UnsupportedFeatureError(
+                'Clause on SELECT statement in simple expression')
+
         return dispatch.compile(stmt.result, ctx=ctx)
 
     parent_ctx = ctx
diff --git a/edb/pgsql/delta.py b/edb/pgsql/delta.py
--- a/edb/pgsql/delta.py
+++ b/edb/pgsql/delta.py
@@ -5911,7 +5911,10 @@ def _alter_innards(
         if self.metadata_only:
             return schema
 
-        if not is_comp:
+        if (
+            not is_comp
+            and (src and has_table(src.scls, schema))
+        ):
             orig_def_val = self.get_pointer_default(prop, orig_schema, context)
             def_val = self.get_pointer_default(prop, schema, context)
 


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
@@ -2001,6 +2001,24 @@ async def test_edgeql_ddl_default_13(self):
                 """
             )
 
+    async def test_edgeql_ddl_default_14(self):
+        await self.con.execute(r"""
+            create type X;
+            insert X;
+        """)
+
+        with self.assertRaisesRegex(
+            edgedb.MissingRequiredError,
+            'missing value for required property',
+        ):
+            await self.con.execute(r"""
+                alter type X {
+                    create required property foo -> str {
+                        set default := (select "!" filter false);
+                    }
+                };
+            """)
+
     async def test_edgeql_ddl_default_circular(self):
         await self.con.execute(r"""
             CREATE TYPE TestDefaultCircular {
@@ -2771,6 +2789,8 @@ async def test_edgeql_ddl_ptr_set_type_using_03(self):
                     set default := '';
                 };
             };
+            # And that aliases don't either
+            create alias Alias := Foo;
         """)
 
         await self.con.execute(r"""