Replies: 1 comment
-
I am having a very similar issue. Let's say I have a let store: entities::stores::Model = entities::stores::Entity::find()
.filter(entities::stores::Column::Id.eq(1))
.one(&ctx.db).await?
.ok_or(Error::NotFound)?;
let books_paginator = store.find_related(entities::books::Entity)
.order_by_desc(entities::books::Column::Name)
.paginate(&ctx.db, 10); Now that we have books, we need to load the reviews for each book one by one, introducing the n+1 problem. I fixed it for now, using the following: let store: entities::stores::Model = entities::stores::Entity::find()
.filter(entities::stores::Column::Id.eq(1))
.one(&ctx.db).await?
.ok_or(Error::NotFound)?;
let books_paginator = store.find_related(entities::books::Entity)
.order_by_desc(entities::books::Column::Name)
.paginate(&ctx.db, 10);
let books: Vec<entities::books::Model> = books_paginator.fetch_page(1).await?;
let reviews: Vec<Vec<entities::reviews::Model>> = books.load_many(entities::reviews::Entity, &ctx.db);
for (books, reviews) in books.into_iter().zip(reviews.into_iter()) {
// now I have books and their reviews, using only 2 queries
} |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi! I like using Sea, but there are a number of problematic points that are difficult to solve in work projects.
They mainly concern relational queries. For example, I'm currently trying to figure out how to implement pagination after a find_with_related query.
Mistake: Method
paginate
not found in the current scope for typeSelectTwoMany<Entity, Entity>
.Beta Was this translation helpful? Give feedback.
All reactions