Skip to content
Discussion options

You must be logged in to vote

For the first part, I’d probably not use find_also_related if you need custom filtering on the joined/related row.

find_also_related is nice for simple relation loading, but once you need conditions on the related alias, manual join is usually clearer.

Example idea:

use sea_orm::{
    sea_query::Alias,
    ColumnTrait, EntityTrait, JoinType, QueryFilter, QuerySelect,
};

let override_alias = Alias::new("food_override");

let row = foods::Entity::find()
    .filter(foods::Column::Id.eq(food_id))
    .filter(foods::Column::Source.ne(Sources::User))
    .join_as(
        JoinType::LeftJoin,
        foods::Relation::SelfRef.def(),
        override_alias.clone(),
    )
    // condition on the …

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by im-trisha
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants