Brendonovich / prisma-client-rust

Type-safe database access for Rust
https://prisma.brendonovich.dev
Apache License 2.0
1.84k stars 108 forks source link

which type should be used when apply include in find #201

Closed swaitw closed 1 year ago

swaitw commented 1 year ago
use crate::{prisma::{PrismaClient, user::{Data as User, self}}};
pub async fn find_user_by_email(client:&PrismaClient, email: String) -> Option<User> {
    let user = client.user().find_unique(user::email::equals(email)).include(user::include!({
    profile
    })).exec().await.unwrap();
    user
}

here will show unmatched type. so does anyone know how to fix it? thanks.

Brendonovich commented 1 year ago

Looks like you've misspelled profile as profille. Would that be what's causing the problem?

swaitw commented 1 year ago
sorry, the spelling is right in my code, but show error like that: user ^^^^ expected struct prisma::user::Data, found struct find_user_by_email_and_password::{closure#0}::Data
Brendonovich commented 1 year ago

Ah, you'll want to check out this section of the docs for using include! types as function return types.

swaitw commented 1 year ago

sorry, thanks for your answer but a little bit confused here, do i need create these type manulaly ? if not, where can i find them.

Brendonovich commented 1 year ago

You'll want to do something like this:

use crate::{prisma::{PrismaClient, user::{Data as User, self}}};

user::include!(user_with_profile { profile });

pub async fn find_user_by_email(client: &PrismaClient, email: String) -> user_with_profile::Data {
  let user = client
    .user()
    .find_unique(user::email::equals(email))
    .include(user_with_profile::include())
    .exec()
    .await
    .unwrap()
}
swaitw commented 1 year ago

works for me, you save my life, thanks a lot for that.