Repository: edgedb/edgedb

Issue:
InternalServerError: uncompiled expression in the field 'default' of property
EdgeDB Version: EdgeDB 1.0-beta.1+g2eee1afe4.d20210301

Changing schema from:
```
type Post {
  required property createdAt -> datetime {
    default := datetime_current();
  }

  required link user -> User;

  required property content -> str {
    constraint min_len_value(1);
    constraint max_len_value(280);
  }
}
```
to:
```
abstract type Event {
  required property createdAt -> datetime {
    default := datetime_current();
  }

  required link user -> User;
}

type Post extending Event {
  required property content -> str {
    constraint min_len_value(1);
    constraint max_len_value(280);
  }
}
```

Run `edgedb create-migration`:
```
did you create object type 'default::Event'? [y,n,l,c,b,s,q,?]
y
did you alter object type 'default::Post'? [y,n,l,c,b,s,q,?]
y
ERROR: InternalServerError: uncompiled expression in the field 'default' of property <QualName default::__|createdAt@default|Post>
  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 "/usr/lib/x86_64-linux-gnu/edgedb-server-1-beta1/lib/python3.9/site-packages/edb/server/compiler_pool/worker.py", line 312, in worker
          res = meth(*args)
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-beta1/lib/python3.9/site-packages/edb/server/compiler_pool/worker.py", line 198, in compile_in_tx
          units, cstate = COMPILER.compile_in_tx(cstate, *args, **kwargs)
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-beta1/lib/python3.9/site-packages/edb/server/compiler/compiler.py", line 2004, in compile_in_tx
          return self._compile(ctx=ctx, source=source), ctx.state
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-beta1/lib/python3.9/site-packages/edb/server/compiler/compiler.py", line 1545, in _compile
          return self._try_compile(ctx=ctx, source=original)
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-beta1/lib/python3.9/site-packages/edb/server/compiler/compiler.py", line 1602, in _try_compile
          comp, capabilities = self._compile_dispatch_ql(ctx, stmt)
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-beta1/lib/python3.9/site-packages/edb/server/compiler/compiler.py", line 1495, in _compile_dispatch_ql
          self._compile_and_apply_ddl_stmt(ctx, ql),
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-beta1/lib/python3.9/site-packages/edb/server/compiler/compiler.py", line 734, in _compile_and_apply_ddl_stmt
          delta = s_ddl.delta_from_ddl(
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-beta1/lib/python3.9/site-packages/edb/schema/ddl.py", line 582, in delta_from_ddl
          _, cmd = _delta_from_ddl(
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-beta1/lib/python3.9/site-packages/edb/schema/ddl.py", line 629, in _delta_from_ddl
          schema = cmd.apply(schema, context)
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-beta1/lib/python3.9/site-packages/edb/schema/delta.py", line 3323, in apply
          schema = self._alter_innards(schema, context)
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-beta1/lib/python3.9/site-packages/edb/schema/delta.py", line 2979, in _alter_innards
          schema = op.apply(schema, context=context)
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-beta1/lib/python3.9/site-packages/edb/schema/delta.py", line 3323, in apply
          schema = self._alter_innards(schema, context)
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-beta1/lib/python3.9/site-packages/edb/schema/delta.py", line 2979, in _alter_innards
          schema = op.apply(schema, context=context)
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-beta1/lib/python3.9/site-packages/edb/schema/delta.py", line 3002, in apply
          schema = self._alter_begin(schema, context)
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-beta1/lib/python3.9/site-packages/edb/schema/referencing.py", line 1556, in _alter_begin
          schema = self.inherit_fields(
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-beta1/lib/python3.9/site-packages/edb/schema/inheriting.py", line 172, in inherit_fields
          result = self.compile_expr_field(
        File "/usr/lib/x86_64-linux-gnu/edgedb-server-1-beta1/lib/python3.9/site-packages/edb/schema/delta.py", line 2487, in compile_expr_field
          raise errors.InternalServerError(
      edb.errors.InternalServerError: uncompiled expression in the field 'default' of property <QualName default::__|createdAt@default|Post>
Rolling back last operation...
did you alter object type 'default::Post'? [y,n,l,c,b,s,q,?]
```


Discussion:

Resolving patch:
diff --git a/edb/schema/links.py b/edb/schema/links.py
--- a/edb/schema/links.py
+++ b/edb/schema/links.py
@@ -552,6 +552,7 @@ class AlterLinkLowerCardinality(
 
 class AlterLinkOwned(
     referencing.AlterOwned[Link],
+    pointers.PointerCommandOrFragment[Link],
     referrer_context_class=LinkSourceCommandContext,
     field='owned',
 ):
diff --git a/edb/schema/lproperties.py b/edb/schema/lproperties.py
--- a/edb/schema/lproperties.py
+++ b/edb/schema/lproperties.py
@@ -367,6 +367,7 @@ class AlterPropertyLowerCardinality(
 
 class AlterPropertyOwned(
     referencing.AlterOwned[Property],
+    pointers.PointerCommandOrFragment[Property],
     referrer_context_class=PropertySourceContext,
     field='owned',
 ):


Test patch:
diff --git a/tests/test_schema.py b/tests/test_schema.py
--- a/tests/test_schema.py
+++ b/tests/test_schema.py
@@ -5246,6 +5246,31 @@ def test_schema_migrations_drop_parent_07(self):
             type Child;
         """])
 
+    def test_schema_migrations_drop_owned_default_01(self):
+        self._assert_migration_equivalence([r"""
+            type Foo;
+            type Post {
+                required property createdAt -> datetime {
+                    default := datetime_current();
+                }
+                link whatever -> Foo {
+                    default := (SELECT Foo LIMIT 1);
+                }
+            }
+        """, r"""
+            type Foo;
+            abstract type Event {
+                required property createdAt -> datetime {
+                    default := datetime_current();
+                }
+                link whatever -> Foo {
+                    default := (SELECT Foo LIMIT 1);
+                }
+            }
+
+            type Post extending Event;
+        """])
+
     def test_schema_migrations_computed_optionality_01(self):
         self._assert_migration_equivalence([r"""
             abstract type Removable {