Skip to content

Nested relations filtering doesn't work #3890

Closed

Description

@trixden

Issue type:

[ ] question
[x] bug report
[ ] feature request
[ ] documentation issue

TypeORM version:

[ ] latest
[ ] @next
[x] 0.2.15 (or put your version here)

Steps to reproduce or a small repository showing the problem:

I have 2 models

category.entity.ts

@Entity()
export class BlogCategoryEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Index()
  @Column({unique: true})
  alias: string;

  @Column()
  title: string;

  @OneToMany(type => BlogPostEntity, post => post.category)
  posts: BlogPostEntity[]
}

post.entity.js

@Entity()
export class BlogPostEntity {
  @PrimaryGeneratedColumn()
  id: number;

  @Index()
  @Column({unique: true})
  alias: string;

  @Column()
  title: string;

  @Column('text')
  description: string;

  @Column({type: 'text', nullable: true})
  text: string;

  @Index()
  @Column()
  isPublished: boolean;

  @Index()
  @Column()
  publishedAt: Date;

  @Column()
  categoryId: number;

  @ManyToOne(type => BlogCategoryEntity, category => category.posts, {eager: true, nullable: false, onDelete: 'CASCADE'})
  @JoinColumn()
  category: BlogCategoryEntity;
}

When i filter query by category_id, everything is ok:

const [rows, count] = await this.repository.findAndCount({
  skip: query.skip,
  take: query.take,
  where: {
    category: {
      id: 1
    }
  }
});

But, filter works only by primary nested field. Filtering by other fields don't work:

const [rows, count] = await this.repository.findAndCount({
  skip: query.skip,
  take: query.take,
  where: {
    category: {
      alias: 'test'
    }
  }
});
SELECT
  "BlogPostEntity"."id" AS "BlogPostEntity_id",
  "BlogPostEntity"."alias" AS "BlogPostEntity_alias",
  "BlogPostEntity"."title" AS "BlogPostEntity_title",
  "BlogPostEntity"."description" AS "BlogPostEntity_description",
  "BlogPostEntity"."text" AS "BlogPostEntity_text",
  "BlogPostEntity"."isPublished" AS "BlogPostEntity_isPublished",
  "BlogPostEntity"."publishedAt" AS "BlogPostEntity_publishedAt",
  "BlogPostEntity"."categoryId" AS "BlogPostEntity_categoryId",
  "BlogPostEntity_category"."id" AS "BlogPostEntity_category_id",
  "BlogPostEntity_category"."alias" AS "BlogPostEntity_category_alias",
  "BlogPostEntity_category"."title" AS "BlogPostEntity_category_title"
FROM "blog_post_entity" "BlogPostEntity"
LEFT JOIN "blog_category_entity" "BlogPostEntity_category"
  ON "BlogPostEntity_category"."id"="BlogPostEntity"."categoryId"
WHERE "BlogPostEntity"."categoryId" = ? -- PARAMETERS: [null]

Are there options to implement this?

Activity

  1. vivirancy commented on May 29, 2019

    @vivirancy

    @trixden Did you find any way around this yet? I just ran into this exact issue today and so far have not found a clean work around.

  2. trixden commented on Jun 7, 2019

    @trixden
    Author
  3. anitricks commented on Jun 7, 2019

    @anitricks
  4. trixden commented on Jun 7, 2019

    @trixden
    Author
  5. anitricks commented on Jun 7, 2019

    @anitricks
  6. trixden commented on Jun 7, 2019

    @trixden
    Author
  7. vivirancy commented on Jun 10, 2019

    @vivirancy

    It would be nice to see a solution that works with TypeORM. I would hate to replace an ORM in a production app just to get this working. So far I've had to avoid any nested where statements. If anything its actually produced some cleaner code, but it would be nice to have to omit certain records without added API endpoints/resolvers for each unique case.

  8. haschu commented on Jun 11, 2019

    @haschu
    Contributor

    I guess this is more a feature request than a bug, because TypeORM doesn't seems to support nested where conditions, or am I wrong?

    I can't find anything in the docs.

  9. hector-chauran commented on Jul 15, 2019

    @hector-chauran

    You can always use the query builder.

    const [rows, count] = await this.repository.manager.createQueryBuilder('post')
        .innerJoinAndSelect('post.category', 'category')
        .where('category.alias = :alias', { alias: 'test' })
        .getManyAndCount();
    SELECT
        "post"."id" AS "post_id",
        "post"."alias" AS "post_alias",
        "post"."title" AS "post_title",
        "post"."description" AS "post_description",
        "post"."text" AS "post_text",
        "post"."isPublished" AS "post_isPublished",
        "post"."publishedAt" AS "post_publishedAt",
        "post"."categoryId" AS "post_categoryId",
        "category"."id" AS "category_id",
        "category"."alias" AS "category_alias",
        "category"."title" AS "category_title"
    FROM "blog_post_entity" "post"
    INNER JOIN "blog_category_entity" "category"
        ON "category"."id"="post"."categoryId"
    WHERE "category"."alias" = $1
    -- PARAMETERS: ["test"]
  10. frieck commented on Aug 8, 2019

    @frieck

    So, Typeorm supports or not nested relations filtering??
    Having the same problem here, but it is not clear if this is a bug (and will be fixed) or this is a desired feature...

  11. haschu commented on Aug 8, 2019

    @haschu
    Contributor

    @frieck It depends on whether you want to use FindOptions or QueryBuilder. As for QueryBuilder, since you're able to build any query you like, you can filter nested relations as deep as you want. Using FindOptions, this is currently not supported :/

    I submitted a feature request for this (#4280), but unfortunately there isn't much feedback yet.

    As this feature was also crucial for my team, we started to implement a TypeOrm extension wrapper, that also supports nested relation filtering using FindOptions. We're going to publish it here on GitHub (see also: #534) as soon as we can find some time - which is hopefully very soon :).

  12. frieck commented on Aug 8, 2019

    @frieck

    @haschu That feature is absolutely crucial! Even if QueryBuilder can be used to build the most complex queries (and we use it alot!) some times it lacks for simplicity, and I believe the FindOptions were built for that simplicity.

    Would be glad if I can take a look in your extension wrapper! I have no idea on how does it works...

    Is there any workaround to fix that, or maybe we can just wait for your feature request to be released, or even your wrapper?

  13. haschu commented on Aug 8, 2019

    @haschu
    Contributor

    I don't think there is a workaround - we actually did implement a custom Entity and Repository with custom FindOptions to achieve this feature. Our extension is currently tied to in-house code, hence I cannot share it for now, but I'll do my best to outsource the essentials into a standalone project soon.

    Currently, the active TypeORM development is kind of on hold - but it sure will going on (#3267)
    I strongly believe that eventually those feature are going to be implemented into TypeORM, we just have to wait a bit :)

  14. chris-snow commented on Aug 22, 2019

    @chris-snow

    Oddly enough, nested relations filtering works when using findOne.

  15. 31 remaining items

  16. haschu commented on Oct 9, 2020

    @haschu
    Contributor

    @imnotjames This isn't a bug, but a missing feature. See #4280 🙂

  17. imnotjames commented on Oct 9, 2020

    @imnotjames
    Contributor

    I automatically added labels based in the first post of the issue

  18. imnotjames commented on Oct 15, 2020

    @imnotjames
    Contributor

    I tested this a few different ways. This is supported already in some cases and this issue is a duplicate of a previous issue.

  19. imnotjames commented on Oct 15, 2020

    @imnotjames
    Contributor

    Duplicate of #2707

  20. vash72 commented on Oct 16, 2020

    @vash72

    I tested this a few different ways. This is supported already and this issue is a duplicate of a previous issue.

    Maybe i'm wrong but for me ins't supported @imnotjames

     const result = await repository.find({
          where: {
            // job: { name: 'Pulizia stoviglie' }
          },
          relations: ['job']
        });
    
        Logger.debug(result);
    
    --> [
      {
        "id": "d65dfbd3-c0ce-4a1f-a190-dee060c2dc8f",
        "jobId": "4f3754d9-07d6-49be-8787-0fa7dbebedf5",
        "shiftId": "b5dc4ef8-6f54-4c88-b5e9-bf2ed1acac57",
        "job": {
          "createdAt": "2020-10-12T10:34:57.878Z",
          "updatedAt": "2020-10-13T09:19:23.922Z",
          "deletedAt": null,
          "id": "4f3754d9-07d6-49be-8787-0fa7dbebedf5",
          "name": "Pulizia stoviglie",
          "colorId": 5
        }
      }
    ]
    

    if i try with nested where

     const result = await repository.find({
          where: {
             job: { name: 'Pulizia stoviglie' }
          },
          relations: ['job']
        });
    
        Logger.debug(result);
    
    -->[]
    
    
  21. nicobytes commented on Nov 9, 2020

    @nicobytes

    Find with embedded entities doest work when runs SQL query, TypeORM take the obj like an id no like the embedded entity's attribute.

    appointmentsCoachesRepo.find({
          relations: ['appointment'],
          where: {
            appointment: {
              status: 'pending'
            }
          },
        });
    

    In the log:

    Screen Shot 2020-11-09 at 5 46 41 PM

    In the WHERE "AppointmentsCoaches"."appointmentId" = $1 therefore return []

    cc @vash72 @imnotjames

    Note: I'm using the version 0.2.29

  22. YEMEAC commented on May 3, 2021

    @YEMEAC

    still not working.

  23. huybkdn23 commented on May 24, 2021

    @huybkdn23

    Still not working now, we still need this feature

  24. pundarikas commented on May 29, 2021

    @pundarikas

    Not working.

  25. arayik-yervandyan commented on Jun 16, 2021

    @arayik-yervandyan

    Still not working . version ^7.1.5

  26. Yuri-Ribeiro commented on Jun 23, 2021

    @Yuri-Ribeiro

    Still not working.

  27. imnotjames commented on Jun 23, 2021

    @imnotjames
    Contributor

    Still a duplicate of #2707 - direct conversation there.

  28. locked and limited conversation to collaborators on Jun 23, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions