Repository: edgedb/edgedb

Issue:
Sequence default fails in migration
### Discussed in https://github.com/edgedb/edgedb/discussions/3493

<div type='discussions-op-text'>

<sup>Originally posted by **tchak** February 15, 2022</sup>
I have the following schema:

```
module default {
  scalar type ProjectNumber extending sequence;

  type Project {
    required property number -> ProjectNumber {
      constraint exclusive;
    }
    required property name -> str;
  }
}
```

I expect `number` to be autofilled. But when I use this schema from TypeScript driver type signature refuses to let me create a Project without specifying a `number` property. I tried the following schema but it failed on migration:

```
module default {
  scalar type ProjectNumber extending sequence;

  type Project {
    required property number -> ProjectNumber {
      constraint exclusive;
      default := sequence_next(introspect ProjectNumber);
    }
    required property name -> str;
  }
}
```

Is this is a bug or intended behaviour ?</div>


Discussion:
```
        File "/home/elvis/dev/edgedb/edgedb/edb/pgsql/delta.py", line 3061, in get_pointer_default
          default_value = schemamech.ptr_default_to_col_default(
        File "/home/elvis/dev/edgedb/edgedb/edb/pgsql/schemamech.py", line 417, in ptr_default_to_col_default
          sql_expr = compiler.compile_ir_to_sql_tree(ir, singleton_mode=True)
        File "/home/elvis/dev/edgedb/edgedb/edb/pgsql/compiler/__init__.py", line 129, in compile_ir_to_sql_tree
          raise errors.InternalServerError(*args) from e
      edb.errors.InternalServerError: no IR compiler handler for <class 'edb.ir.ast.TypeIntrospection'>
```
Thanks for quick answer!
So once the bug is resolved if I want correctly typed behaviour in the query builder I should use the second version of the schema? The documentation makes it sound as if autoincrement behaviour is enabled without explicit default with a call to `sequence_next`.
Yes, sequence scalars have an implicit default, you don't need to specify it explicitly.  The querybuilder needs to be adjusted to be aware of the fact.

Resolving patch:
diff --git a/edb/pgsql/compiler/expr.py b/edb/pgsql/compiler/expr.py
--- a/edb/pgsql/compiler/expr.py
+++ b/edb/pgsql/compiler/expr.py
@@ -315,6 +315,14 @@ def compile_SliceIndirection(
     return result
 
 
+@dispatch.compile.register(irast.TypeIntrospection)
+def compile_TypeIntrospection(
+        expr: irast.TypeIntrospection, *,
+        ctx: context.CompilerContextLevel) -> pgast.BaseExpr:
+    raise errors.UnsupportedFeatureError(
+        'type introspection not supported in simple expressions')
+
+
 def _compile_call_args(
     expr: irast.Call, *,
     ctx: context.CompilerContextLevel


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
@@ -12523,6 +12523,19 @@ async def test_edgeql_ddl_set_abstract_bogus_01(self):
                 alter type Foo set abstract;
             """)
 
+    async def test_edgeql_ddl_no_type_intro_in_default(self):
+        with self.assertRaisesRegex(
+                edgedb.UnsupportedFeatureError,
+                r"type introspection not supported in simple expressions"):
+            await self.con.execute(r"""
+                create scalar type Foo extending sequence;
+                create type Project {
+                    create required property number -> Foo {
+                        set default := sequence_next(introspect Foo);
+                    }
+                };
+            """)
+
 
 class TestConsecutiveMigrations(tb.DDLTestCase):
     TRANSACTION_ISOLATION = False