Do child/parent relationship names need to be unique?

Don’t all child relationship names on an object need to be unique ? I know they need to be unique if it’s a relationship name on a parent to child relationship, but I thought they still needed to be unique if it is was a child-to-parent relationship, or even just lookup fields. This is in reference to custom objects.

I’ve asked around and read the documentation and seem to be getting different two different answers on this and I’m still confused after reading the documentation. Can the community give a definitive answer?

Answer

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.

Attribution
Source : Link , Question Author : Zoom_v , Answer Author : Community

Leave a Comment