Do child/parent relationship names need to be unique?
Data Model and Terminology
Given an object Parent__c
and object Child__c
with a lookup to Parent__c
.
Field Name
API Name for the lookup field on Child__c
, i.e. Child__c.Parent_Lookup__c
Parent Relationship Name
Use to reference fields on the parent from the child object, i.e. String parentName = child.Parent_Lookup__r.Name
. Derived from field name.
Child Relationship Name
Used to reference children from the parent, i.e. List<Child__c> children = parent.Children__r;
Uniqueness Constraints
Child relationship names must be unique for the parent. Meaning that there can't be more than one lookup to the parent object (doesn't matter what the child object is) for each child relationship name.
Conversely, parent relationship names, must be unique for each child object. Meaning there can't be more than one lookup on the child object with the same name (doesn't matter what the parent object is).
Examples
So if you had two account lookups on the case object, you couldn't use Cases__r
for both child relationship names and instead would need to change one to Cases1__r
. You'll notice that Salesforce automatically adds a numeral for you if the parent object already has a child relationship name with the child object's plural label.
Discussion
If it helps, you can think about whether something would need to be unique or not, by thinking about whether it would lead to a SOQL statement that could be interpreted two different ways which would make life for the database pretty tough.
Gotchas - Non-Unique Parent and Child Relationship
As @bryan points in his answer Salesforce doesn't check that a field relationship doesn't have the same base as a child relationship. This can cause issues it can't tell if obj.Value__r
is looking for the custom field Value__c
or the child relationships Value__r
. By convention child relationships are plural so you'd need to change the default value to a singular version for this to happen.
I am assuming this is in reference to your previous question about relationships Parent and Parent.Id with custom objects
The relationship names to be unique for that object. So say you have a custom object cus__c and lookup field to an Account. To access the Account fields from the cus__c record you would use
cus__c.Account__r.FieldName
If you added another lookup to an account, you would not be able to name it Account__c, as that field already exists. So you could do something like Account2__c, and then to access that accounts fields you would use
cus__c.Account2__r.FieldName