edgedb-python icon indicating copy to clipboard operation
edgedb-python copied to clipboard

Backlinks and type filter operator in ORM

Open vpetrovykh opened this issue 7 months ago • 2 comments

We need a way to access backlinks in ORM. As we know the backlinks need a name and a source type to be properly defined. However in pure EdgeQL we allow bare backlinks (which then get inferred as Object type) and we treat the backlink .< operator separate from type filtering [is Foo].

Should we have __backlink__ available in Python? Do we pass the type as an argument?

q = default.User.filter(lambda u: u.__backlink__.members(default.Group).name = 'admin')

Will we have some kind of separate type filter operator exposed in Python? Can I translate something like:

select User {
  name,
  # assume the schema has type Bot extending User
  special := .friends[is Bot],
}

It could be std.is_type(...) maybe? Would that mean that backlinks then become something like: std.is_type(u.__backlink__.friends, default.Bot) or std.is_type(u.__backlink__.members, default.Group)?

Ideally, I'd prefer not to pollute the plain fields of reflected types with all the backlinks directly, hence the __backlink__ intermediary.

vpetrovykh avatar Jun 09 '25 18:06 vpetrovykh

FWIW, in the TypeScript query builder we do pollute the main fields with all possible backlinks and it's not a huge deal. Helps with discoverability, too. u[".<friends[is Bot]"] is what that looks like. People find it just as offputting as backlinks in general, but doesn't seem to be an actual hinderance once they get used to it.

scotttrinh avatar Jun 09 '25 18:06 scotttrinh

We use the stuff we reflect as pydantic types to also specify fields in constructors or in validators. Polluting these with all the backlinks is not a good idea IMO.

vpetrovykh avatar Jun 09 '25 19:06 vpetrovykh