TO ONE vs TO MANY Associations in ABAP CDS View Entities

60 min read
TO ONE vs TO MANY Associations
Understand why TO ONE and TO MANY Associations behave differently, how they affect generated SQL, and why Cardinality is much more than documentation.
Click image to enlarge

Association Masterclass

Lesson 6 of 15 – TO ONE vs TO MANY Associations

In the previous lesson we learned that Cardinality represents the business relationship between two business objects.

This naturally leads to another important question.

If Cardinality is only metadata, why does SAP care whether an Association is TO ONE or TO MANY?

The answer lies in how the CDS compiler interprets your Association and the assumptions it can safely make while generating SQL.

Learning Objectives

By the end of this lesson you will be able to:

  • Understand the difference between TO ONE and TO MANY Associations.
  • Know why the CDS compiler treats them differently.
  • Understand why TO MANY Associations have additional restrictions.
  • Recognize typical business scenarios for each Cardinality.
  • Prepare for generated SQL behavior in the next lesson.

Prerequisites

  • Lessons 1–5 of this Association Masterclass.
  • Understanding of Cardinality.
  • Basic Association Navigation.

Introduction

From a business perspective, the difference between TO ONE and TO MANYseems obvious.

However, from the CDS compiler's perspective, the difference is much more significant.

A TO ONE Association guarantees that navigation can never return more than one target record.

A TO MANY Association provides no such guarantee.

That single difference affects compiler validation, navigation, generated SQL and even which CDS constructs are permitted.

Architect Perspective

Don't think of TO ONE and TO MANY as syntax.

Think of them as promises made to the CDS compiler.

TO ONE says:
"This navigation can never return more than one record."

TO MANY says:
"Multiple records may exist. Plan accordingly."

Understanding TO ONE Associations

A TO ONE Association guarantees that each source record is related to at most one target record.

TO ONE Association
ABAP CDS
association [0..1] to I_Customer as _Customer

on $projection.SoldToParty = _Customer.Customer

Business interpretation:

One Sales Order has at most one Sold-To Customer.

Since only one Customer can exist, the compiler can safely navigate fields such as:

Navigation
ABAP CDS
_Customer.CustomerName

Understanding TO MANY Associations

A TO MANY Association represents a relationship where multiple target records may exist.

TO MANY Association
ABAP CDS
association [0..*] to I_SalesDocumentItem as _Item

on $projection.SalesDocument = _Item.SalesDocument

Business interpretation:

One Sales Order may contain many Sales Order Items.

Since multiple Items may exist, navigation behaves differently and the compiler cannot make the same assumptions that it can for TO ONE Associations.

Why the CDS Compiler Cares

Imagine the compiler receives the following navigation.

Path Expression
ABAP CDS
_Customer.CustomerName

The compiler immediately knows that this navigation can return only a single CustomerName because the Association is TO ONE.

Now consider:

TO MANY Navigation
ABAP CDS
_Item.Material

The compiler now faces a completely different situation.

One Sales Order may contain several Materials because multiple Items can exist.

This uncertainty is the reason TO MANY Associations have additional restrictions compared with TO ONE Associations.

🧠

INSIDE THE CDS COMPILER

Reading Cardinality

šŸ’­ Compiler Thought Process

The compiler evaluates the declared Cardinality before deciding how navigation should behave.

Compiler Decision Flow

1

Read the Association.

2

Determine whether it is TO ONE or TO MANY.

3

Check the requested path expression.

4

Estimate how many target records may exist.

5

Choose the appropriate navigation semantics.

6

Continue SQL generation.

āœ… Final Compiler Decision

Cardinality directly influences how the compiler interprets Association navigation.

⚔ SQL Optimization Insight

Correct Cardinality gives the compiler accurate information. Incorrect Cardinality may lead to unexpected navigation behavior and inefficient SQL.

Why TO MANY Associations Have Restrictions

This is one of the most frequently asked questions during interviews.

If Associations are just relationships, why are TO MANY Associations more restricted than TO ONE Associations?

The answer is simple.

The compiler cannot predict which of the many matching records should be used.

Consider the following relationship:

Sales Order → Items
Text
Sales Order 50000001

↓

Item 10

↓

Item 20

↓

Item 30

Suppose you write:

Path Expression
ABAP CDS
_Item.Material

Which Material should be returned?

  • Item 10?
  • Item 20?
  • Item 30?

The compiler has no deterministic answer.

That uncertainty is exactly why TO MANY Associations are treated differently.

Why TO MANY Associations Cannot Always Be Used in WHERE

One of the most common syntax errors developers encounter is trying to use a TO MANY Association inside a WHERE condition.

Example
ABAP CDS
where _Item.Material = 'TG11'

This is not supported because the compiler cannot guarantee that the Association returns only one record.

If three matching items exist, which one should be compared with'TG11'?

Architect Perspective

This behavior is not an arbitrary CDS restriction.

It exists to prevent ambiguous SQL semantics and protect developers from accidentally producing incorrect results.

Generated SQL Perspective

Let's compare how the compiler views two Associations.

TO ONETO MANY
Maximum one matching record.Multiple matching records possible.
Safe for scalar navigation.May produce duplicate source rows.
Compiler has deterministic semantics.Compiler must consider multiplicity.

Real SAP Examples

AssociationCardinalityWhy?
Sales Order → CustomerTO ONEA Sales Order has only one Sold-To Customer.
Sales Order → ItemsTO MANYA Sales Order may contain many Items.
Billing Document → Accounting ItemsTO MANYOne Billing Document can create multiple Accounting Entries.

Real Project Example: Why SAP Designed Associations This Way

Imagine the same Interface View is consumed by three different applications.

• A Fiori List Report needs only Customer Name.
• A RAP service needs Sales Order Items.
• An Analytical Query needs both.

SAP defines both TO ONE and TO MANY Associations once. Each consumer navigates only the Associations it requires.

This makes SAP's Virtual Data Model highly reusable while allowing the CDS compiler to generate SQL appropriate for each consumer.

Architect Perspective

Technical Architect Insight

Many developers think TO ONE and TO MANY exist only for documentation.

They don't.

The CDS compiler actively uses Cardinality information to validate path expressions, determine navigation semantics and optimize generated SQL.

Declaring incorrect Cardinality is effectively providing incorrect metadata to the compiler.

šŸ’” SAP Best Practice

Always model Cardinality according to the business relationship.

Never declare a TO ONE Association simply to bypass syntax restrictions.

If the business relationship is genuinely TO MANY, model it as TO MANY and design your CDS View accordingly.

āŒ Common Mistakes

  • Using TO ONE because it 'works'.
  • Ignoring the actual business relationship.
  • Using TO MANY Associations inside unsupported WHERE conditions.
  • Assuming Cardinality affects only documentation.
ExperiencedInterview Question

Why are TO MANY Associations more restricted than TO ONE Associations?

Answer: Because multiple target records may exist. The CDS compiler cannot always determine a single deterministic result for navigation, filtering or scalar expressions.

ArchitectInterview Question

Can incorrect Cardinality affect compiler behavior?

Answer: Yes. The CDS compiler trusts the declared Cardinality during validation and SQL generation. Incorrect Cardinality provides incorrect metadata, which can lead to invalid assumptions and unexpected behavior.

šŸ’” Key Takeaway

TO ONE and TO MANY are much more than different Cardinality values.

They tell the CDS compiler whether navigation is guaranteed to return at most one record or whether multiple matching records are possible.

This distinction directly influences compiler validation, generated SQL, supported language constructs and overall CDS behavior.

In the next lesson, we'll compare Associations with traditional SQL JOINs and identify when each approach is most appropriate in ABAP Cloud development.