Self Referential Many to Many Relationship

I created a Many to Many relationship to and from the User entity- nothing out of the ordinary. I then tried to programmatically retrieve the records associated to this relationship (ie, to the intersect table). I specified the following intersect rule:


LinkEntity filteringLink = new LinkEntity();
filteringLink.LinkFromEntityName = relationshipName;
filteringLink.LinkFromAttributeName = secondaryEntityPK;
filteringLink.LinkToEntityName = secondaryEntityName;
filteringLink.LinkToAttributeName = secondaryEntityPK;
filteringLink.LinkCriteria = filter;

LinkEntity link = new LinkEntity();
link.LinkFromEntityName = primaryEntityName;
link.LinkFromAttributeName = primaryEntityPK;
link.LinkToEntityName = relationshipName;
link.LinkToAttributeName = primaryEntityPK;
link.LinkEntities = new LinkEntity[] { filteringLink };

But I kept getting an error saying: ” ‘geni_followedby’ entity doesn’t contain attribute with Name = ‘systemuserid’.\n Platform\n”.

Looking at the database revealed the obvious issue- because it’s a self referential relationship, both key’s have the same name (systemuserid), so they get renamed to systemuseridone and systemuseridtwo. So I changed my code as follows (quick hack):


LinkEntity filteringLink = new LinkEntity();
filteringLink.LinkFromEntityName = relationshipName;
filteringLink.LinkFromAttributeName = secondaryEntityPK + "two";
filteringLink.LinkToEntityName = secondaryEntityName;
filteringLink.LinkToAttributeName = secondaryEntityPK;
filteringLink.LinkCriteria = filter;

LinkEntity link = new LinkEntity();
link.LinkFromEntityName = primaryEntityName;
link.LinkFromAttributeName = primaryEntityPK;
link.LinkToEntityName = relationshipName;
link.LinkToAttributeName = primaryEntityPK + "one"
link.LinkEntities = new LinkEntity[] { filteringLink };