简体   繁体   中英

error: cannot find attribute `table_name` in this scope

I want to do a page query using rust diesel, I am using this code to do a unit test in rust:

#[cfg(test)]
mod test {

    use std::env;
    use diesel::{Connection, ExpressionMethods, PgConnection, QueryDsl, RunQueryDsl};
    use rust_wheel::common::query::pagination::PaginateForQuerySource;
    use crate::model::diesel::rhythm::rhythm_schema::favorites::dsl::favorites;
    use crate::model::diesel::rhythm::rhythm_schema::favorites::like_status;
    use crate::models::Favorites;

    #[test]
    fn page_test(){
        use crate::model::diesel::rhythm::rhythm_schema::favorites::dsl::*;
        use rust_wheel::common::query::pagination::{PaginateForQueryFragment, PaginateForQuerySource};
        let conn = establish_music_connection();
        let query = favorites
            .filter(like_status.eq(1))
            .paginate(1)
            .per_page(10)
            .load::<Favorites>(&conn)
            .expect("query fav failed");

        println!("{:?}", 1);
    }

    pub fn establish_music_connection() -> PgConnection {
        let database_url = std::env::var("MUSIC_DATABASE_URL").expect("MUSIC_DATABASE_URL must be set");
        PgConnection::establish(&database_url).expect(&format!("Error connecting to {}", database_url))
    }
}

shows error like this:

error: cannot find attribute `table_name` in this scope
  --> src/models.rs:15:3
   |
15 | #[table_name = "favorites"]
   |   ^^^^^^^^^^

this is my models.rs define, this two models are store in different database:

use rocket::serde::Serialize;
use serde::Deserialize;
use crate::model::diesel::dolphin::dolphin_schema::dashboard;
use crate::model::diesel::rhythm::rhythm_schema::favorites;

#[derive(Insertable, Serialize, Queryable, Deserialize,Default)]
#[table_name = "dashboard"]
pub struct Dashboard {
    pub id: i32,
    pub app_count: i32,
    pub user_count: i32
}

#[derive(Serialize, Queryable, Deserialize,Default)]
#[table_name = "favorites"]
pub struct Favorites {
    pub id: i64,
    pub song_id: String,
    pub created_time: i64
}

why did this happen? what should I do to fix it?

Only the Insertable derive macro handles the #[table_name =...] attribute. So it should not be included if you're not using it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM