Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Selecting specific relationship fields #2593

Closed
RoyLee opened this issue Jan 2, 2022 · 4 comments
Closed

Selecting specific relationship fields #2593

RoyLee opened this issue Jan 2, 2022 · 4 comments

Comments

@RoyLee
Copy link

RoyLee commented Jan 2, 2022

Describe the bug
Hello, I am trying to use the entity manager to query for specific fields in the relationship data. In my case I am trying to query for all of the author's books but only want the title values. Right now I am getting both the title and genre values. Can you please assist me in figuring out why this is happening?

Author.ts

@Entity({ tableName: "authors" })
export class Author extends BaseEntity {
  @Property()
  @Unique()
  name: string;

  @OneToMany('Book', 'author')
  books = new Collection<book>(this);

  constructor(name: string) {
    super();
    this.name = name;
  }
}

Book.ts

@Entity({ tableName: "books" })
export class Book extends BaseEntity {
  @Property()
  @Unique()
  title: string;

  @Property()
  genre: string;

  @ManyToOne('Author')
  author!: Author;

  constructor(title: string, genre: string) {
    super();
    this.title = title;
    this.genre = genre;
  }
}

Code that I am calling:

const author = await em().findOne(Author,
  authorId, {
  populate: { books: LoadStrategy.SELECT_IN, },
  fields: ['name', 'books.title'],
});

Expected behavior
I expect only the book title values and not the genre values.

Versions

| Dependency | Version |
| node | v16.13.1 |
| typescript | v4.3.5 |
| mikro-orm | v4.5.9 |
| mongodb | v4.5.9 |

@RoyLee
Copy link
Author

RoyLee commented Jan 2, 2022

One way I can think of doing this is to do 2 separate queries. One to get the author id and then another one to query books for that specific author id. This creates 2 calls to the database which is not ideal.

@RoyLee
Copy link
Author

RoyLee commented Jan 2, 2022

Hmm, looks like this is already happening when I used the query in my example. It is running 2 separate queries to the database. Can this be only one?

@RoyLee
Copy link
Author

RoyLee commented Jan 2, 2022

Just tried running:

    const books = await em().findAndCount(Book,
      { author: author.id },
      {
        fields: ['title'],
      });

Thought this returns both genre and title fields. Is my syntax missing anything?

@RoyLee
Copy link
Author

RoyLee commented Jan 2, 2022

Is this issue related to these bugs?
#1707
#1739

@mikro-orm mikro-orm locked and limited conversation to collaborators Jan 2, 2022
@B4nan B4nan converted this issue into discussion #2594 Jan 2, 2022

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant