LLBLGen 5.O - SD.LLBLGen.Pro.ORMSupportClasses.ORMRelationException: 'Relation at index 1 doesn't contain an entity already added to the FROM clause. Bad alias?

Posts   
 
    
jbm
User
Posts: 5
Joined: 07-Jul-2021
# Posted on: 07-Jul-2021 15:36:42   

Hi !

This is my model

Entity_A_Parent
ID_A (PK)
Entity_B_Child
ID_A (PK)
Entity_C_Link
ID (PK)
ID_B (PK)

I use GetMultion Entity_B_Child. When I tried to add relation to Entity_C_Link relations.Add(Entity_B_Child.Relations.Entity_C_LinkEntityUsingEntity_B_ChildId_A);

When the app run Entity_B_Child.GetMulti(filter, maxTotalRecordCount, defaultSort, relations, startIndex_1base, pageSize);

An exception has throwed : SD.LLBLGen.Pro.ORMSupportClasses.ORMRelationException: 'Relation at index 1 doesn't contain an entity already added to the FROM clause. Bad alias?'

Thanks for your help.

Update:

Model:

Animal (Parent)
ID (PK)
Cat (Child)
ID (PK)
CatKeyword
ID (PK)
CAT_ID (FK)
KEYWORD_ID (FK)
Keyword
ID (PK)
public CatCollection SearchEvents(Criteria criteria)
{
    var cats = new CatCollection();
    IRelationCollection relations = new RelationCollection();
    IPredicateExpression filter = new PredicateExpression();

    if (critere.Keywords.Any())
    {
        relations.Add(CatKeywordEntity.Relations.CatEntityUsingCatId);
        var filterKeyword = new FieldCompareRangePredicate(CatKeywordFields.KeywordId, critere.Keywords);
        filter.AddWithAnd(filterKeyword);
    }

    int pageNumber = criteria.PageIndex + 1;
    int pageSize = criteria.PageSize;

    int startIndex = (pageNumber > 1) ? checked((pageNumber - 1) * pageSize) : 0;
    int startIndex_1base = startIndex + 1;

    SortExpression defaultSort = new SortExpression(new SortClause(CatFields.Date, SortOperator.Descending));

    int maxTotalRecordCount = SetMaxRecordSize(pageSize);
    cats.GetMulti(filter, maxTotalRecordCount, defaultSort, relations, startIndex_1base, pageSize);
    int totalRecordCount = Math.Min(cats.GetDbCount(filter, relations), maxTotalRecordCount);

    return cats;
}

Items In relation collection :

[0] {CatKeyworkEntity.(CatId) m:1 CatEntity.(Id)} [1] {AnimalEntity.(Id) 1:1 CatEntity.(Id) (Hierarchy relation)}

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 07-Jul-2021 19:15:23   

Are A and B in an inheritance Hierarchy? Could you please post the complete fetch code?

jbm
User
Posts: 5
Joined: 07-Jul-2021
# Posted on: 08-Jul-2021 10:06:09   

Are A and B in an inheritance Hierarchy?

Yes

Could you please post the complete fetch code?

Yes, I updated threads.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39613
Joined: 17-Aug-2003
# Posted on: 08-Jul-2021 20:48:37   
Frans Bouma | Lead developer LLBLGen Pro
jbm
User
Posts: 5
Joined: 07-Jul-2021
# Posted on: 09-Jul-2021 14:46:57   

Otis wrote:

Please post the SQL query executed. You can obtain the query using tracing, see https://www.llblgen.com/Documentation/5.0/LLBLGen%20Pro%20RTF/Using%20the%20generated%20code/gencode_troubleshootingdebugging.htm

Yes I has activated tracing. But any queries displayed...

I found a workaround with QuerySpec instead.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39613
Joined: 17-Aug-2003
# Posted on: 10-Jul-2021 09:20:44   

Ok, so we can close this thread? For completeness, what's the workaround you're using? (As queryspec creates the same relationcollection objects you're using)

Frans Bouma | Lead developer LLBLGen Pro
jbm
User
Posts: 5
Joined: 07-Jul-2021
# Posted on: 16-Jul-2021 14:28:37   

Sorry for my answer so late.

I used this workaround :

var cats = new CatCollection();
var queryFactory = new QueryFactory();
EntityQuery<CatEntity> catQuery = queryFactory.Cat.As("CustomCats");
catQuery.From(QueryTarget.InnerJoin(queryFactory.CatKeyword)
                    .On(CatFields.Id == CatKeywordFields.CatId));
catQuery.Where(CatKeywordFields.Id.In(catKeywordIds));
cats.GetMulti(catQuery);

Before close this thread have a solution for the firts method or are you recommend to use my workaround ?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39613
Joined: 17-Aug-2003
# Posted on: 17-Jul-2021 09:47:43   

The original query should work fine (our tests succeed) so we don't know why it fails. Nevertheless, your queryspec variant is recommended so you should keep using the workaround. The As("CustomCats") isn't needed btw.

Frans Bouma | Lead developer LLBLGen Pro
jbm
User
Posts: 5
Joined: 07-Jul-2021
# Posted on: 19-Jul-2021 09:26:29   

OK, thanks for your time. I keep the query spec solution.

I close this thread.