-
-
Notifications
You must be signed in to change notification settings - Fork 496
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
OneToMany property find with populate + fields #2651
Comments
It might be worth trying this in 5.x. I know some changes went in to be able to infer the populate statement via the fields, so you could try: const author = await this.authorRepository.find({}, {
fields: ['name', 'books.title'],
}); |
I updated to 5.x and ran your example but it returns the same results as example 2 where it throws no exception but returns empty books. The fields argument matches similar to example 2 so decide to try using code example 1 in 5.x without the populate: const author = await this.authorRepository.find({}, {
fields: ['name', 'books', 'books.title'],
}); This also returns the same error as what I reported |
That's weird, we even have pretty much the same test case in the codebase: Maybe try dropping the (definitely something to fix, agreed this is quite confusing) |
Thanks for clarifying that when using a virtual property for a OneToMany that I should not need to have the property name in in fields. So I went back and did some further investigation on this and it seems that it works fine using the JOINED loading strategy (which is support from 5.x onwards). const author = await this.authorRepository.find({}, {
fields: ['name', 'books.title'],
strategy: LoadStrategy.JOINED,
}); As SELECT_IN is default (also only supported for partial loading in 4.5.x) it seems to fail to load the children into the entity after debugging. Due to lack of knowledge I was able to track that it was querying the data correctly back from postgres within the function populateMany in EntityLoader but when it returns the data back to populateField. The data returned doesnt seemed to be used or there is a bug within populateMany where it is not hydrating the entity? |
That test I linked is using select-in strategy, it works just fine there. You can even see the assertions for queries issued.
So if you see some problem with that, provide a reproduction for it. I consider this issue to be just about the 'ignorance of 1:m property in But it sounds like you already had that entity loaded in the context. You can't expect partial loading to "erase what was already loaded". If you had that entity fully loaded, partial loading won't strip those "extra properties". |
I have investigated this further and discovered that when using select_in we have to explicitly include the link property in the children for it to work. working example const author = await this.authorRepository.find(
{},
{
populate: ['books'],
fields: ['name', 'books.author', 'books.title'],
},
); not working example const author = await this.authorRepository.find(
{},
{
populate: ['books'],
fields: ['name', 'books.title'],
},
); So when not including |
Well, it is not needed for joined strategy to work, as there is just one query, you dont work with the FK explicilty. For select-in, you need to explicitly load that FK, so we have its value that is required in the following population query. |
If that is the case I am happy with the answer but could you add that part into the documentation that select-in requires its dependent field when using the field find option. |
Let's keep this open, at least the problem in the OP is definitely someting we can easily fix just by ignoring the 1:m property in |
Describe the bug
When performing a find query where I set the populate on a OneToMany property and select certain fields on find options I get the error "Cannot read properties of undefined (reading '0')"
Stack trace
To Reproduce
Steps to reproduce the behavior:
Entities -
Fail Example 1 - throws above exception
Fail Example 2 - throw no exception but returns empty books
Working Example - Returns data but returns all fields
I've created a repo which recreates the problem https://github.com/tsangste/mikro-orm-populate-fields
Expected behavior
Expected to populate the OneToMany field and select the appropriate fields to return back
Versions
The text was updated successfully, but these errors were encountered: