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

find_with_related does not return records with no related entity. It just records that have more than one related entities. #1800

Closed
hamedai opened this issue Aug 3, 2023 · 8 comments

Comments

@hamedai
Copy link

hamedai commented Aug 3, 2023

  1. Create two entities named A and B. A has many Bs. So, B is A's related entity.
  2. Create an instance of A that has no B.
  3. Now try to retrieve this instance with the following query:
a_model::Entity::find().filter(some filters)
                .find_with_related(b_model::Entity)
                .all(db)
                .await?
                .first()
                .map(|t| t.to_owned())
                .into()

Expected Behavior

return A with an empty list of Bs

Actual Behavior

no A has been returned

Reproduces How Often

Always

Workarounds

Reproducible Example

Versions

@tyt2y3
Copy link
Member

tyt2y3 commented Aug 4, 2023

It sounds like left join vs inner join. However find_with_related always calls left join. Can you provide a reproducible example?

@hamedai
Copy link
Author

hamedai commented Aug 4, 2023

I have checked the generated SQL. It is using left join. and by looking at the logs, it seems that the result has one response.
The example:
This a public repository I was working on: https://github.com/bkonkle/rust-example-caster-api
users and role_grants have such a relation, they are located in lib/domains.
In the graphQL router when it is checking the credential by this code:

            .get_by_username(username, &true)
            .await
            .unwrap_or(None)

It will try to use the problematic feature I mentioned before.
/lib/domains/users/service.rs:get_by_username@line 80

            query
                .find_with_related(role_grant_model::Entity)
                .all(&*self.db)
                .await?
                .first()
                .map(|t| t.to_owned())
                .into()

Note that the issue does not appear on version 11.*, it is just on version 12.0.1.

@tyt2y3
Copy link
Member

tyt2y3 commented Aug 4, 2023

Thank you for reporting. I think I have a clue now. You pointed out a regression.

tyt2y3 added a commit that referenced this issue Aug 4, 2023
@hamedai
Copy link
Author

hamedai commented Aug 4, 2023

No problem!
I'm looking forward to the next version!

@tyt2y3
Copy link
Member

tyt2y3 commented Aug 4, 2023

Please try 0.12.2

@tyt2y3
Copy link
Member

tyt2y3 commented Aug 4, 2023

@hamedai appreciate if you can let me know if it's good

@hamedai
Copy link
Author

hamedai commented Aug 4, 2023

Thanks, Chris!
I have tested it and it works fine now.

@tyt2y3 tyt2y3 closed this as completed Aug 4, 2023
@tyt2y3
Copy link
Member

tyt2y3 commented Aug 4, 2023

Thank you. Note to self: we'd need to add more test cases.

@SeaQL SeaQL locked as resolved and limited conversation to collaborators Aug 4, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants