ERPClimb logoERPClimb
CDS Associations
RAP / CDS / ODatabeginner 8 min read

CDS Associations Part 1: Source, Target, ON Condition and Cardinality

Free sample lesson · open to everyone

Key takeaway

Declare CDS associations correctly: source and target, the ON condition, $projection, cardinality, and exposing the association so consumers can use it.

In this lessonShow sections

The situation: a real project scenario

A project had one "universal" sales order view with inner joins to customer, material, plant and status texts. Every consumer — list report, API and three analytics queries — paid for all joins, and orders with a missing plant disappeared entirely because of the inner join.

The team rebuilt it as an interface view with associations and only the header fields. Each consumer now follows only the associations it needs. The list report became several times faster, and the "missing orders" defect vanished because unused associations are no longer joined at all.

Explanation

This series covers associations across five parts: this lesson covers declaring an association; Part 2 covers path expressions and how associations become joins; Part 3 covers to-one versus to-many semantics and performance; Part 4 covers associations versus compositions in RAP; and Part 5 covers troubleshooting duplicates, missing data and slow queries. CDS Views Parts 1 through 3 are the prerequisite.

A join says "combine these two sources now." An association says "these two entities are related like this" and leaves it to the consumer to decide whether to actually follow the relationship. That's what makes a single view reusable: a list that only needs order numbers never pays for the customer join, while an object page can follow _Customer to display the name.

The syntax breaks down piece by piece. In "association [0..1] to ZI_Customer as _Customer on $projection.Customer = _Customer.Customer", [0..1] is the cardinality — how many target rows exist per source row, covered further in Part 3. "to ZI_Customer" names the target, which in clean designs is another CDS entity rather than a table directly.

"as _Customer" gives it an alias, with the convention being to start it with an underscore. The "on" clause is the join condition, where the source side uses $projection.<element> (the element name in the current view) or the source alias, and the target side uses _Alias.<element>.

Every source field used in the ON condition with $projection has to also be in the element list.

Declaring the association isn't enough on its own for consumers to use it — it needs to be added to the element list as well, as in { …, Customer, _Customer }. Only then can other views, ABAP SQL and OData follow it. If the association is only needed inside the current view, say to read a single field, it can be used in a path expression without being exposed at all.

Cardinality has a fixed notation: [0..1] or [1] means to-one, at most one or exactly one target row; [0..*] or [*] means to-many; and [1..*] means at least one. Leaving it out defaults to [0..1], but it should never be left to that default — it should always be written explicitly. Newer releases also offer a more readable cardinality syntax, such as "association of many to one," which means exactly the same thing and can be used where the system supports it.

Associations can carry more than simple key equality in their ON condition — for example, a language condition, as in "association [0..1] to ZI_OrderStatusText as _StatusText on $projection.OverallStatus = _StatusText.Status and _StatusText.Language = $session.system_language." A condition like this is what turns what would otherwise be a to-many text table into a to-one association.

In practice: open ZI_SalesOrder from the CDS Views lessons, add associations to the customer, the items and the status text as shown in the code example, expose them in the element list, and activate. Then open Data Preview, select a row, and use "Follow Association" to navigate — without any join having been written by hand. With that, an association can be declared, exposed and navigated; Part 2 shows exactly what SQL gets generated when it's actually used.

Technical illustration & lab

CDS ASSOCIATIONS ACADEMY · PART 1ERPClimb.com

Source, target and the ON relationship

Entity relationship · cardinality as a data contract

Source: ZI_Order

OrderIdCustomerId
5001C01
5002C02
ON CustomerId = CustomerId

Target: ZI_Customer

CustomerIdName
C01Acme
C02Beta
Source

One row from the order view is the starting point.

Target

The associated customer view is reached by its key.

[0..1]

For each source order, expect zero or one matching target.

define view entity ZI_Order as select from zorder as src
  association [0..1] to ZI_Customer as _Customer
    on src.CustomerId = _Customer.CustomerId
{ key src.OrderId, src.CustomerId, _Customer }
Why expose the association?The exposed _Customer lets consumers navigate the declared relationship. The mere declaration is not the same as selecting target fields in a join.
Illustrative SAP CDS data relationships and deterministic teaching examples. Exact SQL planning depends on SAP release, database, authorization and query usage.

Code example

ABAP Code
// ===== Step 1: associations in the interface view =====@AccessControl.authorizationCheck: #CHECK@EndUserText.label: 'Sales Order'define view entity ZI_SalesOrder  as select from zso_head   // to-one: each order has at most one customer  association [0..1] to ZI_Customer       as _Customer    on $projection.Customer = _Customer.Customer   // to-many: an order has many items  association [0..*] to ZI_SalesOrderItem as _Item    on $projection.SalesOrder = _Item.SalesOrder   // to-one thanks to the language condition  association [0..1] to ZI_OrderStatusText as _StatusText    on  $projection.OverallStatus = _StatusText.Status    and _StatusText.Language      = $session.system_language{  key sales_order    as SalesOrder,      customer_id    as Customer,        // used in ON -> must be in the element list      overall_status as OverallStatus,   // used in ON -> must be in the element list      order_date     as OrderDate,      currency_code  as CurrencyCode,       // Step 2: expose the associations for consumers      _Customer,      _Item,      _StatusText} // ===== Step 3: consumers decide what to follow =====// ABAP SQL - join to customer ONLY because it is used here://   SELECT SalesOrder, \_Customer-CustomerName AS CustomerName//     FROM zi_salesorder INTO TABLE @DATA(orders).//// OData - navigation only when requested://   GET .../SalesOrder('4711')?$expand=_Item

Watch out

Common mistakes

  • Using hard joins where an association would let consumers decide.
  • Using a field with $projection in the ON condition without adding it to the element list.
  • Forgetting to expose the association, so consumers cannot navigate.
  • Omitting cardinality and relying on the default.
  • Declaring a text association without a language condition, making it to-many.
  • Pointing associations at tables instead of CDS entities.

Do this

Best practices

  • Model relationships as associations in interface views.
  • Always state cardinality explicitly and correctly.
  • Name associations with a leading underscore and a business name.
  • Add language or validity conditions to make text and time-dependent relations to-one.
  • Expose associations that consumers need; keep internal ones unexposed.

In the interview

Interview angle

Questions: "What is a CDS association?", "How is it different from a join?", "What is $projection?", "Why must you specify cardinality?"
Strong answer: an association is a declared relationship that becomes a join only when used (path expression or $expand). $projection refers to elements of the current view in the ON condition, and those elements must be in the element list. Cardinality documents the business relation and influences correctness and optimization; the default [0..1] should never be relied on. Mention text associations with a language condition as a practical example.

Practice this lesson

Quick check

0 of 3 questions attempted

Question 1 of 3

In an association ON condition, a source field is referenced with $projection. What must be true?

Practise this in an interview

Answer in your own words and get instant feedback, the way an interviewer would judge it.

1What design rules do you follow for associations in an interface view layer?

2What is a CDS association, how is it declared, and how does it differ from a join?

Try a related Code Lab

This published exercise covers the same SAP topic. Apply the ideas in a hands-on scenario, then return to review your answers.

Open: Correlated Subquery Instead of an Association in a CDS View →

See it in code

Each example starts from a requirement, shows the design, then the code step by step with a plain-language explanation of every part.

Was this lesson helpful?

Keep exploring

Check before use: lesson examples are educational, not a guarantee of correctness for your SAP edition or release. Sample code has not been executed on your SAP system. Confirm API availability, syntax, authorizations and business impact in an approved development or test environment.