Filtering by a related record
A condition can ask whether a related record exists:
exists(<Entity>.<reference>, <condition>). The reference links the two
entities, and the condition tests the related record's own fields. Here
a help desk tags enquiries through a link entity, EnquiryTag, and lists a
page of the enquiries carrying a tag:
use desk/entities { Deskhand, Enquiry, Tag }/// A tag a deskhand put on an enquiry, which only they see, until they take/// it off.entity EnquiryTag { id: Uuid enquiry_id: Enquiry.id { on_delete: OnDelete.cascade } tag_id: Tag.id { on_delete: OnDelete.cascade } deskhand_id: Deskhand.id { on_delete: OnDelete.cascade } removed_at: Instant? lifecycle: { visible: removed_at == none } policy: { read: [deskhand_id] }}
use desk/entities { Enquiry, EnquiryTag, Tag }use desk/types { EnquiryRow }/// The enquiries the caller tagged `tag_id`, a page at a time, by title.query enquiries_tagged( tag_id: Tag.id, after: PageCursor?, caller: Principal.user) -> Page<EnquiryRow> { from: Enquiry where: exists(EnquiryTag.enquiry_id, EnquiryTag.tag_id == tag_id) order: [Enquiry.title asc] page: { size: 2 after: after }}
use desk/entities { Deskhand, Enquiry, EnquiryTag, Tag }use desk/types { EnquiryRow }/// `enquiry` tagged `tag` by `deskhand`.fixture tag_enquiry(enquiry: Enquiry, tag: Tag, deskhand: Deskhand) -> EnquiryTag { return make.EnquiryTag { enquiry_id: enquiry.id tag_id: tag.id deskhand_id: deskhand.id }}test "a page of the enquiries a deskhand tagged, in title order" { var ann: Deskhand = make.Deskhand { handle: "ann" } var bob: Deskhand = make.Deskhand { handle: "bob" } var pressing: Tag = make.Tag { name: "pressing" } var someday: Tag = make.Tag { name: "someday" } var boiler: Enquiry = make.Enquiry { title: "Fix the boiler" } var chimney: Enquiry = make.Enquiry { title: "Sweep the chimney" } var hedge: Enquiry = make.Enquiry { title: "Trim the hedge" } var porch: Enquiry = make.Enquiry { title: "Paint the porch" } tag_enquiry(boiler, pressing, ann) tag_enquiry(chimney, pressing, ann) tag_enquiry(hedge, pressing, ann) tag_enquiry(porch, someday, ann) tag_enquiry(porch, pressing, bob) var first: Page<EnquiryRow> = enquiries_tagged(pressing.id, none, Principal.user(ann)) assert list.map(first.items, t -> t.title) == ["Fix the boiler", "Sweep the chimney"] var second: Page<EnquiryRow> = enquiries_tagged(pressing.id, first.next, Principal.user(ann)) assert list.map(second.items, t -> t.title) == ["Trim the hedge"] assert second.next == none var bobs: Page<EnquiryRow> = enquiries_tagged(pressing.id, none, Principal.user(bob)) assert list.map(bobs.items, t -> t.title) == ["Paint the porch"]}
- The reference. Either way round: another entity's reference to
the one the query reads asks for a record that refers here, as
EnquiryTag.enquiry_iddoes; one of the query's own entity's references asks for the record it points at, so a list of todos in a project callednameisexists(Todo.project_id, Project.name == name). Only a reference links two records, soexists(Enquiry.title, ...)isquery.condition_unsupported, naming the references there are. - The condition. Any condition a query's
where:takes, on the related entity's fields: comparisons,in,== none,and,orandnot, and the operations with an exact SQL form. Leave it out when any related record will do:exists(EnquiryTag.enquiry_id)is an enquiry with a tag. Ageo.test, a search inside text andmatchesstay in a query of their own, since Jadpo's code works them out again on the rows a query reads. - None of them.
not exists(EnquiryTag.enquiry_id)keeps the enquiries with no tag.existsstands anywhere a condition does, undernot,orandandtoo, on a query's ownwhere:lines and an aggregate query's, so a count of the enquiries with a tag is one statement. An include'swhere:andexists's own condition don't take another. - Who sees the related records. A related record the caller may not
read, or that the related entity's
visible:hides, isn't there forexists, as it isn't for any read, unlessinclude_hidden: truecomes last inside it, which counts the hidden ones too (Records a lifecycle hides). So above, Ann's page holds only the enquiries she tagged, and a tag she took off counts for nothing. A call with no caller, a test's or a seed's, runs without policy (Calls with no caller), so it sees every linkvisible:lets through. The fieldsexiststests show their values, as any condition's do, so each must be one everyone the related entity'sread:lets in may read (What else shows a field): otherwisepolicy.output_field_unprovednames it. - What it can't link. An entity with itself, another store's or a
Redis entity, and another domain's, whose records come from its public
queries (
query.condition_unsupported). - Paging and order.
existsdecides which records a query reads, before its order and its page, so a page holds as many as it can and its cursor walks every match once. Don't read the related records first and filter withEnquiry.id ina list of their references: that reads every link, and stops atlimit: every's 10,000 (Every record that matches).
The statement (jadpo sql) is one EXISTS holding the link, the
condition, the related entity's visible: and its policy, and the index
derived for it starts with the reference, then the fields the condition
tests with == or in, as an include's children's does:
-- enquiries_taggedSELECT "id", "title"FROM "enquiry"WHERE ($4 IS NULL OR ("title" COLLATE "C", "id") > ($4 COLLATE "C", $5)) AND EXISTS (SELECT 1 FROM "enquiry_tag" WHERE "enquiry_tag"."enquiry_id" = "enquiry"."id" AND "tag_id" = $1 AND "removed_at" IS NULL AND "enquiry_tag"."deskhand_id" = $2) AND $3ORDER BY "title" COLLATE "C" ASC, "id" ASCLIMIT 3CREATE INDEX "enquiry_tag_enquiry_id_tag_id_idx" ON "enquiry_tag" ("enquiry_id", "tag_id") WHERE "removed_at" IS NULL;
Markdown for agents: /docs/filtering-by-a-related-record.md.Built from Jadpo's main branch at commit 9080dc3.