Repository: edgedb/edgedb

Issue:
Adding `required multi link` currently asks for singleton fill_expr
When creating a new required multi link, the migration system requires a singleton expression to be provided.

Starting from 

```
  type User {
    property name -> str;
  }

  type Thing {
    property name -> str;
  }
```

Going to 

```
  type User {
    property name -> str;
+   required multi link things -> Thing;
  }

  type Thing {
    property name -> str;
  }  
```

Here’s the migration interaction:

```
$ edgedb migration create
did you create link 'things' of object type 'default::Person'? [y,n,l,c,b,s,q,?]
> y
Please specify an expression to populate existing objects in order to make link 'things' of object type 'default::Person' required:
fill_expr> select Thing
error: SchemaError: possibly more than one element returned by the USING clause for the alteration of link 'things' of object type 'default::Person', while a singleton is expected
  ┌─ query:3:22
  │
3 │         SET REQUIRED USING (select Thing);
  │                      ^^^^^^^^^^^^^^^^^^^^ error

Rolling back last operation...
did you create link 'things' of object type 'default::Person'? [y,n,l,c,b,s,q,?]
> y
Please specify an expression to populate existing objects in order to make link 'things' of object type 'default::Person' required:
fill_expr> select Thing limit 1
Created ./dbschema/migrations/00010.edgeql, id: m1uzptpu7d2p2iwmvt5efqapeohkgukxvf5qgxgs24d5qxfw2ak72a
$
```


Discussion:

Resolving patch:
diff --git a/edb/schema/pointers.py b/edb/schema/pointers.py
--- a/edb/schema/pointers.py
+++ b/edb/schema/pointers.py
@@ -2468,6 +2468,7 @@ def _alter_begin(
 
         orig_required = scls.get_required(orig_schema)
         new_required = scls.get_required(schema)
+        new_card = scls.get_cardinality(schema)
         is_computed = 'required' in scls.get_computed_fields(schema)
 
         if orig_required == new_required or is_computed:
@@ -2483,7 +2484,7 @@ def _alter_begin(
                     context=context,
                     expr=self.fill_expr,
                     target_as_singleton=True,
-                    singleton_result_expected=True,
+                    singleton_result_expected=new_card.is_single(),
                     expr_description=(
                         f'the USING clause for the alteration of {vn}'
                     ),


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
@@ -11633,6 +11633,25 @@ async def test_edgeql_ddl_new_required_pointer_08(self):
             ]
         )
 
+    async def test_edgeql_ddl_new_required_pointer_09(self):
+        await self.con.execute(r"""
+            CREATE TYPE Foo;
+            INSERT Foo;
+        """)
+
+        await self.con.execute("""
+            ALTER TYPE Foo {
+                CREATE MULTI PROPERTY name -> str {
+                    SET REQUIRED USING ({"hello", "world"})
+                }
+            }
+        """)
+
+        await self.assert_query_result(
+            r'''SELECT Foo {name}''',
+            [{'name': {'hello', 'world'}}]
+        )
+
     async def test_edgeql_ddl_new_required_multi_pointer_01(self):
         await self.con.execute(r"""
             CREATE TYPE Foo;