📖 Read · 20 XP
Lookup vs master-detail (and why it matters)
Lookup vs master-detail
Salesforce has two main ways to relate objects, and choosing between them is a classic exam and interview question.
Lookup
A loose link, like a nullable foreign key.
- The child can exist without a parent (unless you make the field required)
- Delete the parent and, by default, the lookup on the child is cleared
- The child has its own owner and sharing
- Example:
Contact.AccountId(a Contact can exist without an Account)
Master-detail
A tight parent-child bond.
- The child (detail) can't exist without its parent (master)
- Delete the master and its details are deleted too (cascade delete)
- The detail has no owner: security and sharing are inherited from the master
- The master can have roll-up summary fields (COUNT, SUM, MIN, MAX of the children)
- Example:
OpportunityLineItemunderOpportunity
| Lookup | Master-detail | |
|---|---|---|
| Parent required | Optional | Always |
| Cascade delete | No (clears field) | Yes |
| Child owner | Own | Inherited |
| Roll-up summaries | No (needs Flow/Apex) | Yes |
| Max per object | 40 | 2 |
In code: relationship names
Relationships get a relationship name used for traversal in SOQL:
// child → parent: dot notation
SELECT Name, Account.Name FROM Contact
// parent → child: subquery using the child relationship name
SELECT Name, (SELECT LastName FROM Contacts) FROM Account
For custom relationships the name ends in __r: Quest__c.Hero__r.Name.