Skip to content

how to query a self-reference like this #2280

Answered by 799189288
799189288 asked this question in Q&A
Discussion options

You must be logged in to vote

yeah, i finally find the resolution.
here is the answer:
1.first of all, you need to query all category and divide result into two part: one with parentid another without.
2.recursively find the children and assign the value.
i think this should be listed as a example.

pub async fn query_category(db: &DbConn) -> Result<Vec<category::TreeModel>, DbErr> {
        let categories: Vec<category::TreeModel> = category::Entity::find()
            .all(db)
            .await?
            .into_iter()
            .map(|i| i.into())
            .collect();
        let first_categories = categories
            .clone()
            .into_iter()
            .filter(|i| i.category.category_id.is_none()…

Replies: 1 comment

Comment options

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