CDS domain modeling — SAP Guided Tutorial
Free sample lesson · open to everyone
Modeling entities, keys, associations and compositions in CDS: when a child belongs to its parent (composition) versus merely references it (association), and how reuse aspects like cuid and managed keep the model consistent.
In this lessonShow sectionsHide sections
Visual overview
The situation: a real project scenario
A purchase request app stored items in a separate entity with request_ID : String. Deleting a request left orphan items, draft editing of items failed, and the Fiori object page could not create items inline. Remodelling items as Composition of many fixed deep insert, cascade delete and draft in one change.
Explanation
Everything later in CAP — services, handlers, authorization — builds on the domain model defined in db/*.cds, so getting the modeling right here pays off throughout the project.
Entities live inside a namespace, and the reuse aspects from @sap/cds/common save you from reinventing basics: using { cuid, managed, Currency } from '@sap/cds/common'; gives you cuid for a UUID primary key and managed for automatic createdAt/createdBy/modifiedAt/modifiedBy fields.
UUID keys via cuid are the right default for transactional data; natural keys make sense only where the business genuinely owns them, such as ISO currency codes, and even then they carry the risk that a "natural" key sometimes turns out not to be stable — a customer number that later gets renumbered is a classic example.
Associations and compositions both model relationships, but they mean different things. An association, like author : Association to Authors, is a reference to something with its own independent lifecycle — CAP generates the foreign key column (author_ID) for you, and associations let you write path expressions in queries, such as SELECT title, author.name FROM Books.
A backlink association, books : Association to many Books on books.author = $self, works the same way without adding a column. A composition, by contrast, models a true part-of relationship: child records are created, read, and deleted together with the parent.
An inline composition like items : Composition of many { key pos : Integer; book : Association to Books; quantity : Integer; } on an Orders entity means you can deep-insert an order with its items in a single request, deleting the order cascades to delete its items, and draft editing works across the whole composition tree.
The rule of thumb is simple: if a child record makes no sense without its parent, model it as a composition, not an association — modeling line items as a plain association or a free-text foreign key loses deep insert, cascade delete, and draft handling all at once.
Beyond entities and relationships, reusable types (type Amount : Decimal(15,2);) and aspects let you avoid repeating field groups, and declarative validation annotations such as @mandatory, @assert.range, and @assert.target give you input checks without writing handler code. It's also worth keeping UI-specific annotations and labels out of db/ entirely — they belong in app/ or srv/ annotation files, so the domain model stays a clean, UI-agnostic description of the business data.
To practice: model an Orders entity with an inline composition of Items and an association to Customers; POST one order with two items and confirm both tables are populated; delete the order and confirm the items disappear while the customer record remains; then run cds compile db --to sql and read the generated foreign keys. For every relationship in your model, you should be able to say why it's an association or a composition, and exactly what happens to the child record on delete.
Code example
namespace my.orders;using { cuid, managed, Currency } from '@sap/cds/common'; entity Customers : cuid { name : String(80); } entity Orders : cuid, managed { customer : Association to Customers @assert.target; currency : Currency; items : Composition of many { key pos : Integer; product : String(40); quantity : Integer @assert.range: [1, 9999]; price : Decimal(9,2); };}Watch out
Common mistakes
- Using
Association to many Itemsfor line items, so deleting an order leaves orphan items. - Storing foreign keys as free String fields instead of managed associations.
- Putting UI annotations and labels into the db layer.
- Using natural business keys that later need to change (e.g. a customer number that gets renumbered).
Do this
Best practices
- Use cuid and managed from @sap/cds/common for transactional entities.
- Model part-of relationships as compositions so deep insert, cascade delete and draft work.
- Use @assert.target on associations where referential integrity must be checked on write.
In the interview
Interview angle
Expect "association vs composition" in almost every CAP interview. Give the lifecycle rule, a concrete example (order items vs customer), and the runtime consequences: deep insert, cascade delete and draft only work across compositions.
Practice this lesson
Original educational case studies for SAP consultants, not official SAP certification exam questions. Verify product edition and implementation details against the current SAP documentation.
Quick check
0 of 4 questions attempted
Question 1 of 4
Order items should be created, read and deleted together with their order. How should they be modeled?
Question 2 of 4
What does the `managed` aspect from @sap/cds/common add to an entity?
Question 3 of 4
A managed to-one association `author : Association to Authors;` is defined on Books. What appears in the database table?
Question 4 of 4
A developer wants the database to reject orders pointing to non-existent customers when they are written via the service. Which is the declarative CAP option?
Practise this in an interview
Answer in your own words and get instant feedback, the way an interviewer would judge it.
1Explain the difference between an association and a composition in CDS, with an example.
2How do you design keys for a CAP domain model, and when would you not use UUIDs?
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.