Skip to content

Relations ​

A navigation between two entity sets becomes an Atscript DB relation. Loading it with $with sends one OData request with $expand, on both hosts, and every row comes back with its related rows under the navigation's name.

ts
// Moost server: an Atscript DB table; browser: an ODataClient (query, pages, one)
await orders.findMany({
  filter: { CustomerId: "C0001" },
  controls: {
    $select: ["OrderId", "OrderDate"],
    $with: [{ name: "Items", filter: { Product: "HT-1000" }, controls: { $select: ["ItemNo"] } }],
  },
});
// v4: Orders?$filter=CustomerId eq 'C0001'&$select=OrderId,OrderDate
//       &$expand=Items($filter=Product eq 'HT-1000';$select=ItemNo,OrderId)

Over HTTP it is the query syntax of Atscript DB: GET /api/sales/OrderSet/query?$with=Items. See loading relations for the shape of $with.

See it live

The items of order 1091 in the browser app are a table held to the order. Code: OrderScreen.vue.

Two kinds of relation ​

The generator decides per navigation:

A foreign-key relation when the service's referential constraint is a foreign key Atscript DB accepts: it covers the target's whole key, the foreign-key properties are primitive properties of the dependent type with matching types, and neither type takes part in an OData inheritance hierarchy. The properties get @db.rel.FK, the navigation @db.rel.to (to the principal) or @db.rel.from (to the dependents):

atscript
export interface Order {
    // …
    @meta.label 'Items'
    @db.rel.from
    Items?: OrderItem[]
}

export interface OrderItem {
    @meta.id
    @meta.label 'Order'
    @db.rel.FK
    OrderId: Order.OrderId

    @meta.id
    ItemNo: string

    @db.rel.to
    Order?: Order
}

A relation without a foreign key (@db.rel.store) for every other navigation between two entity sets, the ones without any referential constraint included (the public Northwind v4 and TripPin services declare none). The service correlates the rows when it expands the navigation:

atscript
@db.rel.store
Supplier?: Supplier
KindLoads with $withGives a foreign-key value help and filterWritten with the row
@db.rel.to / .fromyesyessee below
@db.rel.storeyesnonever: a value is refused

A navigation without exactly one target table (to a type no entity set serves, such as a contained one) is not emitted and is listed in the trailer of the .as file.

/meta lists the relations with their direction: to and from over a foreign key, store for one without.

Rules of $with ​

  • Every part of an entry reaches the service: the relation's filter (the caller's, and any a controller adds), its $select, its $sort and its nested $with. Nothing in an entry is dropped: what the service cannot express is refused.
  • The related rows keep their key. A selection of a relation answers the selected fields and the key of each related row, at every depth, on both hosts. The rows of the read itself are cut to their own selection (a Moost DB controller adds their key).
  • A filter that can never match (an empty $in, for example a scope with no values for this user) leaves the relation out of the request on both hosts: every row has it empty, [] or null. Nothing like $filter=false is sent. On OData v2 it is refused like any filtered relation.
  • OData v2 has no options inside an expansion except the selection (written Items/ItemNo in the root $select). A filter or a sort of a relation, a scope added by a controller included, is refused before any request with 400 NOT_SUPPORTED on both hosts (browser: a ClientError). It is never sent unscoped.
  • No paging inside $with. $skip and $limit in an entry are refused.
  • One service. A relation is read only to an entity set of the same service.
  • No version token on related rows. The ETag of an expanded entity is not read; read the row itself to get its token.

Relations in writes ​

  • A value in a @db.rel.store navigation is refused on every write (NAV_STORE_WRITE), on both hosts.
  • The browser client creates the related entities of an insert with it, through a navigation with a foreign key, as one OData deep insert. Related entities in an update or replace are refused (UNSUPPORTED_DEEP_UPDATE).
  • The server splits a nested write into one write per row and entity set, as Atscript DB does on every adapter: no OData deep insert, and no rollback when a later row fails. Atscript DB takes a nested write only as deep as the type's @db.depth.limit allows, and a generated type declares none (0): an insert with related rows is refused with 400 DEPTH_EXCEEDED ("Nested write depth 1 exceeds declared @db.depth.limit 0 at 'Items'") until you raise it in an annotate file the server imports:
atscript
// src/odata/sales-ui.as
import { Order } from './sales'

@db.depth.limit 1 // the order and its items, one level down
annotate Order {}

The browser client needs no limit: its deep insert is one OData request.

See Writes and Concurrency for the rest of the write rules.

The browser client filters by a related row on OData v4: $some and $none become any() (negated for $none) on a to-many navigation and a navigation path in $filter on a to-one, guarded by the related entity's presence ((Customer ne null and Customer/Country eq 'NO'), so a row without one never matches; $none is its negation). OData v2 has no such filter.

The server filters a table call by a related row of a to-one navigation, on OData v4: findMany, count and aggregate() send the navigation path, in $filter or in the filter() of the $apply.

ts
// Kilograms ordered per coffee, cancelled orders left out:
// OrderItems?$apply=filter((order ne null and order/status_code ne 'X'))/groupby((coffee_ID),aggregate(quantityKg with sum as kg))
const ordered = await space.getTable(roastery.OrderItem).aggregate({
  filter: { order: { $some: { status_code: { $ne: "X" } } } },
  controls: {
    $groupBy: ["coffee_ID"],
    $select: ["coffee_ID", { $fn: "sum", $field: "quantityKg", $as: "kg" }],
  },
});

A predicate on a to-many navigation is refused with 400 REL_FILTER_NOT_SUPPORTED: filter by the foreign key instead, or read the related set first. A dotted path ({ "Customer.Country": "NO" }) gets Atscript DB's message that it is a navigation path and to use { Customer: { $some: … } }. Over HTTP, Moost DB answers $some in the URL with RELATION_NOT_FILTERABLE unless the relation is annotated @db.rel.filterable (in an annotate block of yours). See Limitations.

Entity sets reached only through a navigation ​

A set the service declares not addressable (OData v2 sap:addressable="false") cannot be read on its own. Reach it through $expand from its parent, and do not give it a controller or a screen of its own. Reads through a navigation path (OrderSet('1')/Items) are not used.

DOs and DON'Ts ​

  • Load related rows with $with — don't read them with a second query per row.
  • Scope a relation in validateControls on the server, by conjoining the scope into the $with entry — transformFilter scopes only the controller's own rows. See Moost Server.
  • On OData v2, keep relations unfiltered and unsorted, or filter the root by a to-one path instead — don't expect a scoped expansion.
  • Read a row by its key to edit it — don't take the version token from an expanded row; it has none.

See also ​

SAP, SAP S/4HANA, SAP Fiori, SAPUI5, SAP BTP and other SAP products and services mentioned herein, as well as their respective logos, are trademarks or registered trademarks of SAP SE (or an SAP affiliate company) in Germany and other countries. Atscript OData is an independent open-source project and is not affiliated with, sponsored or endorsed by SAP SE.
Released under the MIT License.