Relation at index 2 doesn't contain an entity already added to the FROM clause. Bad alias?

Posts   
 
    
wpfeffer
User
Posts: 11
Joined: 07-Oct-2008
# Posted on: 13-Oct-2008 21:54:26   

I'm getting the error in the subject line thrown on the following code:

    public static DataTable RetrieveMyRelationships(Contact contact, string RelationshipType)
    {
        DataAccessAdapter daa = new DataAccessAdapter(cnString);
        ResultsetFields Fields = new ResultsetFields( 8 );
        DataTable dt = new DataTable();
        IPredicateExpression pe = new PredicateExpression();
        IRelationPredicateBucket rpb = new RelationPredicateBucket();

        Fields.DefineField(ContactFields.FirstName, 0, "LeftFirstName", "LeftContact");
        Fields.DefineField(ContactFields.LastName, 1, "LeftLastName", "LeftContact");
        Fields.DefineField(StatusFields.StatusName, 2, "LeftStatusName", "LeftStatus");
        Fields.DefineField(ContactRelationshipFields.ContactRelationshipId, 3, "ContactRelationshipID", "ContactRelationship");
        rpb.Relations.Add(ContactEntity.Relations.StatusEntityUsingStatusId, "LeftContact", "LeftStatus", JoinHint.Inner);
        rpb.Relations.Add(ContactEntity.Relations.ContactRelationshipEntityUsingLeftContactId, "LeftContact", "ContactRelationship", JoinHint.Inner);

        Fields.DefineField(ContactFields.FirstName, 4, "RightFirstName", "RightContact");
        Fields.DefineField(ContactFields.LastName, 5, "RightLastName", "RightContact");
        Fields.DefineField(StatusFields.StatusName, 6, "RightStatusName", "RightStatus");
        Fields.DefineField(ContactRelationshipFields.ContactRelationshipId, 7, "ContactRelationshipID_", "ContactRelationship_");
        rpb.Relations.Add(ContactEntity.Relations.StatusEntityUsingStatusId, "RightContact", "RightStatus", JoinHint.Inner);
        rpb.Relations.Add(ContactEntity.Relations.ContactRelationshipEntityUsingLeftContactId, "RightContact", "ContactRelationship", JoinHint.Inner);

        **daa.FetchTypedList(Fields, dt, rpb);**
        return dt;
    }

The bold, underlined text is the line the error is thrown on. I don't know what the problem could be.

The relationships are:

Status [1]->[m] Contact [1]->[m] ContactRelationship

Inside of ContactRelationship I have:

ContactRelationshipId PK LeftContactId FK (to Contact table) RightContactId FK (to Contact table) ContactRelationshipTypeId FK (to ContactRelationshipType table)

As you can see by the method signature, ultimately I will be accepting as input a Contact and a Relationship Type (the variable types will probably change to better reflect the db later on) and I need to return a list of people who are related to the given Contact and the type of relationship(s) they have.

Thank you for any help you can give.

Wayne E. Pfeffer

DvK
User
Posts: 318
Joined: 22-Mar-2006
# Posted on: 13-Oct-2008 22:21:07   

This kind of error usually pops up when relations are added in a wrong order, so not hierarchical to the db-model. Is that possible ? Like :

-> OrderDetails, Customer, Order

instead of

-> Customer, Order, OrderDetails

Regards, Danny

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 14-Oct-2008 08:42:25   

Also, What's the difference between these two relations?

rpb.Relations.Add(ContactEntity.Relations.ContactRelationshipEntityUsingLeftContactId, "LeftContact", "ContactRelationship", JoinHint.Inner);
...
rpb.Relations.Add(ContactEntity.Relations.StatusEntityUsingStatusId, "RightContact", "RightStatus", JoinHint.Inner);

Exactly the same relation with different aliases. Shouldn't be the same result just adding the fields twice to the resultset? simple_smile

Could you please post the SQL you would like to achieve? Also post your LLBLGenPro build version and runtime libraries version.

David Elizondo | LLBLGen Support Team