kurtbuilds / ormlite

An ORM in Rust for developers that love SQL.
https://crates.io/crates/ormlite
MIT License
216 stars 11 forks source link

`insert` & `update_partial` not found #33

Closed holmofy closed 11 months ago

holmofy commented 11 months ago

insert and update_partial are not public, and this method will not be found when called under another package.

// src/model/account_user.rs
use super::enums::ProductEdition;
use chrono::NaiveDateTime;
use ormlite::{model::*, Result};

#[derive(Clone, Debug, Model)]
#[ormlite(table = "account_user")]
pub struct AccountUser {
    #[ormlite(primary_key)]
    pub id: Option<i64>,
    pub created: NaiveDateTime,
    pub modified: NaiveDateTime,
    pub edition: ProductEdition,
    pub name: String,
    pub email: String,
    pub passwd: String,
    pub locked: bool,
    pub last_login: String,
}
// src/app/user.rs
...
#[post("/")]
async fn register(
    state: web::Data<AppState>,
    ip_addr: Option<web::ReqData<IpAddr>>,
    body: Json<RegisterDTO>,
) -> Result<HttpResponse> {
    let mut db = &state.db;
    let exists = AccountUser::exists_by_email(&db, body.email.as_str()).await?;

    if exists {
        return Err(error::ErrorBadRequest("email exists").into());
    }

    let mut success = AccountUser {
        id: None,
        locked: false,
        edition: ProductEdition::L0,
        email: body.email,
        name: body.name,
        passwd: body.passwd,
        last_login: ip_addr.unwrap().to_string(),
        created: Local::now().naive_local(),
        modified: Local::now().naive_local(),
    }
    .insert(&mut db) // insert not found
    .await?;

    return Ok(HttpResponse::Ok().json("success"));
}

The error is as follows:

no method named `update_partial` found for struct `AccountUser` in the current scope
items from traits can only be used if the trait is in scope

no method named `insert` found for struct `AccountUser` in the current scope
items from traits can only be used if the trait is in scope

I can find this method by calling it in the same file as account_user.rs, so I guess the method generated by the macro is not public

kurtbuilds commented 11 months ago

The methods are public. It appears the issue you're running into is the second part of that error message: items from traits can only be used if the trait is in scope.

In account_user.rs, you've imported ormlite::model::*, which contains the Model (& other) traits, so you have access to those methods. In user.rs, you haven't.

In fact, while trait (pub trait $NAME) can be tagged as public or not, methods within them do not take visibility modifiers. You can think of all methods as being public.

If I misunderstood, please reply with additional detail and I'll re-open the issue.

merlindru commented 6 months ago

I ran into this with "No method named _id found for ..." and changing to

use ormlite::model::*

Fixed it! Thank you.

Maybe we could add this to the docs?

kurtbuilds commented 6 months ago

Happy to add that. Would you mind making a PR for where it should be included?