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

Load many to many #1443

Merged
merged 3 commits into from
Feb 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 42 additions & 3 deletions examples/basic/src/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,10 @@ pub async fn all_about_select(db: &DbConn) -> Result<(), DbErr> {

println!("===== =====\n");

find_many(db).await?;

println!("===== =====\n");

find_one(db).await?;

println!("===== =====\n");
Expand Down Expand Up @@ -77,6 +81,30 @@ async fn find_together(db: &DbConn) -> Result<(), DbErr> {
Ok(())
}

async fn find_many(db: &DbConn) -> Result<(), DbErr> {
print!("find cakes with fruits: ");

let cakes_with_fruits: Vec<(cake::Model, Vec<fruit::Model>)> = Cake::find()
.find_with_related(fruit::Entity)
.all(db)
.await?;

// equivalent; but with a different API
let cakes: Vec<cake::Model> = Cake::find().all(db).await?;
let fruits: Vec<Vec<fruit::Model>> = cakes.load_many(fruit::Entity, db).await?;

println!();
for (left, right) in cakes_with_fruits
.into_iter()
.zip(cakes.into_iter().zip(fruits.into_iter()))
{
println!("{left:?}\n");
assert_eq!(left, right);
}

Ok(())
}

impl Cake {
fn find_by_name(name: &str) -> Select<Self> {
Self::find().filter(cake::Column::Name.contains(name))
Expand Down Expand Up @@ -142,13 +170,24 @@ async fn count_fruits_by_cake(db: &DbConn) -> Result<(), DbErr> {
async fn find_many_to_many(db: &DbConn) -> Result<(), DbErr> {
print!("find cakes and fillings: ");

let both: Vec<(cake::Model, Vec<filling::Model>)> =
let cakes_with_fillings: Vec<(cake::Model, Vec<filling::Model>)> =
Cake::find().find_with_related(Filling).all(db).await?;

// equivalent; but with a different API
let cakes: Vec<cake::Model> = Cake::find().all(db).await?;
let fillings: Vec<Vec<filling::Model>> = cakes
.load_many_to_many(filling::Entity, cake_filling::Entity, db)
.await?;

println!();
for bb in both.iter() {
println!("{bb:?}\n");
for (left, right) in cakes_with_fillings
.into_iter()
.zip(cakes.into_iter().zip(fillings.into_iter()))
{
println!("{left:?}\n");
assert_eq!(left, right);
}
println!();

print!("find fillings for cheese cake: ");

Expand Down
2 changes: 1 addition & 1 deletion src/entity/relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use sea_query::{
use std::fmt::Debug;

/// Defines the type of relationship
#[derive(Clone, Debug)]
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum RelationType {
/// An Entity has one relationship
HasOne,
Expand Down
Loading