Repository: edgedb/edgedb

Issue:
ISE when constraint expression on link and __subject__
<!-- Please search existing issues to avoid creating duplicates. -->
- EdgeDB Version: 1.2+5aecabc 
- OS Version: Linux 5.10.16.3

Error:
`edb.errors.InternalServerError: operator does not exist: uuid <> edgedbpub."b57d6cf2-a794-11ec-8c5f-a53bc1d7262a"`

Steps to Reproduce:

1. `edgedb migration create`
2. `edgedb migrate`

<!-- If the issue is about a query error, please also provide your schema -->
Schema:
```
module default {
    type Post {
        link original -> Post;
        constraint expression on (
            .original != __subject__
        );
    }
}
```

`edgedb instance logs`
```
CRITICAL 305 2022-03-18T19:35:09.983 postgres: the database system is starting up
ERROR 305 2022-03-18T21:12:31.632 postgres: invalid input syntax for type uuid: "..."
CRITICAL 555 2022-03-19T18:39:10.431 postgres: the database system is starting up
ERROR 555 2022-03-19T19:44:18.139 postgres: operator does not exist: uuid <> edgedbpub."aa6c9578-a372-11ec-87c3-c577659b3365"
ERROR 555 2022-03-19T19:44:23.744 postgres: operator does not exist: uuid <> edgedbpub."aa6c9578-a372-11ec-87c3-c577659b3365"
edgedb error: edgedb exited prematurely: exit status: 0 (command-line: "/home/username/.local/share/edgedb/portable/1.1/bin/edgedb-server" "--data-dir" "/ho
me/username/.local/share/edgedb/data/accordeon" "--runstate-dir" "/home/username/.cache/edgedb/run/accordeon" "--port" "10700")
ERROR 2364 2022-03-19T19:47:14.077 postgres: operator does not exist: uuid <> edgedbpub."72f511e1-a793-11ec-83de-c3a612d72bb1"
edgedb error: edgedb exited prematurely: exit status: 0 (command-line: "/home/username/.local/share/edgedb/portable/1.2/bin/edgedb-server" "--data-dir" "/ho
me/username/.local/share/edgedb/data/accordeon" "--runstate-dir" "/home/username/.cache/edgedb/run/accordeon" "--port" "10700")
ERROR 2856 2022-03-19T19:52:58.593 postgres: operator does not exist: uuid <> edgedbpub."43188671-a794-11ec-8c61-43546f9d5b62"
edgedb error: edgedb exited prematurely: exit status: 0 (command-line: "/home/username/.local/share/edgedb/portable/1.2/bin/edgedb-server" "--data-dir" "/ho
me/username/.local/share/edgedb/data/accordeon" "--runstate-dir" "/home/username/.cache/edgedb/run/accordeon" "--port" "10700")
```


Discussion:

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
@@ -733,10 +733,10 @@ def _compile_set_in_singleton_mode(
                 name=[ptr_stor_info.column_name],
                 nullable=node.rptr.dir_cardinality.can_be_zero())
         else:
-            colref = pgast.ColumnRef(
-                name=[
-                    common.edgedb_name_to_pg_name(str(node.typeref.id))
-                ]
-            )
+            name = [common.edgedb_name_to_pg_name(str(node.typeref.id))]
+            if node.path_id.is_objtype_path():
+                name.append('id')
+
+            colref = pgast.ColumnRef(name=name)
 
         return colref


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
@@ -8412,6 +8412,25 @@ async def test_edgeql_ddl_constraint_16(self):
                 INSERT Foo { x := ('1', '2') };
             """)
 
+    async def test_edgeql_ddl_constraint_17(self):
+        await self.con.execute(r"""
+            create type Post {
+                create link original -> Post;
+                create constraint expression ON ((.original != __subject__));
+            };
+        """)
+
+        await self.con.execute("""
+            insert Post;
+        """)
+
+        async with self.assertRaisesRegexTx(
+                edgedb.ConstraintViolationError,
+                r'invalid Post'):
+            await self.con.execute("""
+                update Post set { original := Post };
+            """)
+
     async def test_edgeql_ddl_constraint_check_01a(self):
         await self.con.execute(r"""
             create type Foo {