Skip to content
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

Missing method "find_with_linked" #1242

Closed
nitnelave opened this issue Nov 22, 2022 · 6 comments
Closed

Missing method "find_with_linked" #1242

nitnelave opened this issue Nov 22, 2022 · 6 comments
Milestone

Comments

@nitnelave
Copy link

Motivation

We have find_with_related and find_also_linked, but there's no find_with_linked.
For instance, I want to get all users who are in a group whose name is "admin" (not primary key). I have a "membership" table for the N-N relation. I would want to write:

User::find().find_also_linked(Group).filter(Group::Column::Name.eq("admin")).all(db).await?;

Then there's also the question of inner join vs left join. I know the answer is probably going to be pointing me to https://www.sea-ql.org/SeaORM/docs/advanced-query/custom-select/, but it's just a seemingly missing part of the API.

@tyt2y3
Copy link
Member

tyt2y3 commented Nov 22, 2022

May be related to #1203 ?

@nitnelave
Copy link
Author

Somewhat related, but I'm looking to get a Vec<(User, Vec<Group>)>.

@nitnelave
Copy link
Author

Actually, I was looking at the custom-select page, and even with custom joins and so on, I can't figure out how to get a Vec<(User, Vec<Group>)>: You can't derive FromQueryResult for a struct with Vec inside, and I'm not sure how to aggregate across rows.

@nitnelave
Copy link
Author

So, from what I understand, the only way to do it given the current code (if we want to leverage the link) is to use find_also_linked and then post process the results: we'll get a (User, Some(Group)) for each pair of (user, group), plus a (User, None) for users without groups. If we sort by user, they'll be correctly grouped and we can do some stream processing. Not the prettiest but it works. I still feel like this should be done by the framework.

@tyt2y3
Copy link
Member

tyt2y3 commented Jan 1, 2023

I think this omission is not intentional and this can be added.
@billy1624 I think it should be a matter of refactoring into prepare_select_two_for_linked and constructing a SelectTwoMany?

Edit: it is due to the fact that SelectTwoMany does not use a hash map, and instead only rely on the order of the first entity. So it can't handle multiple joins in the complex case. If we change the implementation, we will be able to cater both. Alternatively, we can maintain two selector implementations.

@tyt2y3 tyt2y3 added this to the 0.12.x milestone Feb 3, 2023
@billy1624 billy1624 linked a pull request Jun 27, 2023 that will close this issue
@darkmmon darkmmon mentioned this issue Jun 29, 2023
4 tasks
@tyt2y3
Copy link
Member

tyt2y3 commented Jul 7, 2023

Closed by #1743

@tyt2y3 tyt2y3 closed this as completed Jul 7, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants