edgedb / edgedb

A graph-relational database with declarative schema, built-in migration system, and a next-generation query language
https://edgedb.com
Apache License 2.0
12.79k stars 392 forks source link

`referenced in BY but not declared in USING` in group by off of backlink #6037

Closed jackfischer closed 9 months ago

jackfischer commented 10 months ago

Steps to Reproduce:

  1. Throws error (with helpful guidance) UnsupportedFeatureError: may not group by a field named id Hint: try 'using id_ := .id'
    group(
    select AuditLog.user
    ) by (.id);
  2. Aliasing as recommended works as expected,
    group(
    select AuditLog.user {id_:=.id}
    ) by (.id_);
  3. Add a backlink in the path,
    group(
    select ActivitySource.<context[is AuditLog].user {id_:=.id}
    ) by (id_);
    error: InvalidReferenceError: variable 'id_' referenced in BY but not declared in USING
    ┌─ <query>:3:7
    │
    3 │ ) by (id_);
    │       ^^^ error

Schema:

type User {}
type AuditLog {
    user: User;
    context: ActivitySource;
}
type ActivitySource {}
raddevon commented 9 months ago

I think this one is actually OK. You just need a leading dot on your by:

group(
  select ActivitySource.<context[is AuditLog].user {id_:=.id}
) by (.id_);

or if you define your alias in using instead, you can forego the leading dot:

group(
  select ActivitySource.<context[is AuditLog].user
) using id_ := .id by (id_);

Feel free to re-open if I've missed something here.

jackfischer commented 9 months ago

Ahhhhh I misinterpreted this as somehow an error coming out of postgres from a corrupt query. EdgeDB also has the concept of using.