13

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?

3 Answers 3

20

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.

5
  • Yikes, you beat me by 12 seconds. Good work lol Commented Oct 30, 2013 at 15:09
  • 1
    I think I confused this in my question. I am talking about multiple child relationship names on the same object referring to DIFFERENT objects. I other words, a custom object (Object_X) is referring to Object_A and Object_B and each relationship name name on Object_X is named Myconnection__r. Shouldn't each of those relationships have its own unique name ? I'm sorry if I'm asking the same question. I just want to make sure I'm asking the right one. Thank you very much again !
    – Zoom_v
    Commented Oct 30, 2013 at 15:19
  • 1
    Gotcha, what you're referring to is the "parent relationship name", and those also must be unique for the child object. The converse is the "child relationship name" which must be unique for the parent object. It doesn't matter which objects are involved just that uniqueness is preserved at those levels Commented Oct 30, 2013 at 22:52
  • @Zoom_v I made some edits to both your question and my answer to address both sides of your question. Is that what you're looking for? Commented Oct 31, 2013 at 0:58
  • Ralph - yes, that's what I was asking about. Thank you very much.
    – Zoom_v
    Commented Oct 31, 2013 at 13:56
10

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
2

It appears that a child relationship name can duplicate a lookup field name in the parent. This can cause issues.

We just ran into a case where Object1 already had a field called Lookup2 to Object2. A new lookup field in Object2 was added to reference back to Object1 with a relationship name of Lookup2 (same as an existing field in Object1). Now our VisualForce pages that referenced Object1.Lookup2__r.field are failing because Object1.Lookup2__r is an array (related list). This was done in some managed code, so I can't rename the new relationship. (The namespace, object and field names have been changed to protect the innocent).

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .