question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

nested joins: regression since v4.6.0?

See original GitHub issue

There seems that a regression was introduced since v4.6.0

Here is my controller:

@Crud(
[...]
query: {
        join: {
            posts: {}
            posts.comments: {}
            posts.comments.user: {}
       }
})
export class PostsController {

And HTTP GET:

http://localhost:3000/posts?join=posts&join=posts.comments&join=posts.comments.user

Error:

Error: Relation with property path comments.user in entity was not found.

It works with v4.5.0

After some debugging, the TypeORM relation path used for user entity is: In v4.5.0: comments.user in v4.6.0: posts.comments.user

Here is the line where the error is thrown: function setJoins in packages/crud-typeorm/src/typeorm-crud.service.ts

builder[relationType](allowedRelation.path, alias);

I don’t know if it can be fixed easily or not, though.

Thanks for your feedback!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:13 (3 by maintainers)

github_iconTop GitHub Comments

7reactions
jbrousseaucommented, May 15, 2020

Oh you’re right, thanks for your feedback. I’ve fixed it like that:

@Crud(
[...]
query: {
        join: {
            posts: {}
            posts.comments: { alias: 'postsComments'}
            posts.comments.user: { alias: 'postsCommentsUser'}
       }
})
export class PostsController {

With the HTTP GET that remains the same. Thanks!

6reactions
jeffreyschultzcommented, May 20, 2020

Even with the alias I am still getting this error.

image

image

Update:

I had to add an alias to ‘sections’, ‘sections.comments’, and ‘sections.comment.replies’, and not just ‘sections.comments’ and ‘sections.comment.replies’.

I am not sure, but this might be a bug in typeorm. I see that instead of breaking down the nested relation into multiple joins with aliases, the entire nested path is passed to Typeorm, and the cause of this issue.

This call to findRelationWithPropertyPath(this.relationPropertyPath) fails with this.relationPropertyPath being comments.replies. I would think that if you are searching within the parent alias, that it would be able to find comments.replies, unless it isn’t capable of handling that level of nesting.

image

Further investigation, that call to findRelationWithPropertyPath(this.relationPropertyPath) is not capable of handling the nesting. In the screen shot, this.relations only contains propertyPaths like sections, or comments–nothing like comments.replies, so this call returns nothing.

image

Update:

It seems this is by design in Typeorm. It wants you to turn that nesting into multiple joins with multiple aliases, such as the snippet below.

const user = await this.repo
      .createQueryBuilder('user')
      .innerJoinAndSelect('user.accounts', 'accounts')
      .leftJoinAndSelect('accounts.expenses', 'expenses')
      .leftJoinAndSelect('expenses.projects', 'projects')
      .where('user.id=:user_id')
      .setParameter('user_id', id)
      .getOne()

Here are the changes I ended up making to @Crud, and maybe its because I didn’t completely know how the aliases affected the other joins, but now it makes sense. It’s a mess right now, but Take note of how I am chaining the joins together with the aliases: sections.comments becomes sections_comments, and sections.comments.replies becomes sections_comments.replies, and so on.

    query: {
      filter: {
        'comments.parentId': {
          $eq: null
        }
      },
      join: {
        package: {
          eager: true
        },
        'package.organizations': {
          eager: true
        },
        owner: {
          eager: true,
          allow: allowedUserFields
        },
        editor: {
          eager: true,
          allow: allowedUserFields
        },
        createdBy: {
          eager: true,
          allow: allowedUserFields
        },
        status: {
          eager: true
        },
        forms: {
          eager: true
        },
        'forms.fields': {
          eager: true
        },
        groups: {
          eager: true
        },
        sections: {
          eager: true
        },
        'sections.parent': {
          eager: true
        },
        'sections.lockedBy': {
          eager: true,
          allow: allowedUserFields
        },
        'sections.comments': {
          alias: 'comments',
          eager: true
        },
        'comments.owner': {
          alias: 'comments_owner',
          eager: true,
          allow: allowedUserFields
        },
        'comments.replies': {
          alias: 'comments_replies',
          eager: true
        },
        'comments_replies.owner': {
          alias: 'comments_replies_owner',
          eager: true,
          allow: allowedUserFields
        },
        'sections.versions': {
          alias: 'versions',
          eager: true
        },
        'sections.history': {
          alias: 'history',
          eager: true
        }
      }
    }

Sorry for the length of this comment, but I am working late on some code needed for tomorrow.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Rails Nested Joins Activerecord with conditions - sql
Looks like you don't need nested joins here. Try to use something like. Event.joins(:store).where(stores: {retailer_id: 2}).
Read more >
What to do AFTER nested cross-validation?
I'd go for lambda.1se . Question #6 [and #4]: In the end, I just want one LASSO logistic regression model, with one unique...
Read more >
How to avoid BroadcastNestedJoin in Spark | by Vikas Singh
Based on the aforementioned parameters, Spark selects one of the join strategy listed below: Broadcast Hash Join; Shuffle hash join; Shuffle ...
Read more >
Lecture 9: Joining multiple datasets
3.1 Overview of mutating joins; 3.2 Practical example; 3.3 Inner joins ... cbirthm <dbl> 12, 10, 3, 5, 1, 7, 3, 10, 9,...
Read more >
An Adaptive Join Regression - ORDER BY (SELECT NULL)
Adaptive joins are a new feature in SQL Server 2017. For adaptive join ... 4. 5. 6. 7. 8. SELECT *. FROM dbo.MY_FIRST_CCI...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found