简体   繁体   中英

Rust, Diesel -- what needs to be imported to get the belonging_to function visible in a different scope

提示:本站收集StackOverFlow近2千万问答,支持中英文搜索,鼠标放在语句上弹窗显示对应的参考中文或英文, 本站还提供   中文简体   中文繁体   中英对照 版本,有任何建议请联系yoyou2525@163.com。

New to diesel, and I'm working through the getting started demo, but adding associations with a user who can post. When I try to use the belonging_to() function, it gives the following error:

no function or associated item named belonging_to found for struct diesel_demo::models::Post in the current scope function or associated item not found in diesel_demo::models::Post

I used the Associations macro on the struct, and I thought that was enough to get the belonging_to function working, but I'm missing something.

Here is a simple query where I try to use belonging_to(), but this fails.

#[macro_use] extern crate diesel_demo;

use diesel::prelude::*;
use crate::diesel_demo::*;
use crate::diesel_demo::models::*;

fn main() {
    use diesel_demo::schema::posts::dsl::*;
    use diesel_demo::schema::users::dsl::*;

    let connection = establish_connection();
    let test_id = 2;
    let user = users.find(test_id).first::<User>(&connection).expect("error getting user");
    
    let post_list = Post::belonging_to(&user)
        .load::<Post>(&connection)
        .expect("Error loading user posts");

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

}

And here is the models.rs file where the structs live.

use super::schema::{posts, users};

#[derive(Identifiable, Queryable, PartialEq, Debug)]
#[diesel(table_name = users)]
pub struct User {
    pub id: i32,
    pub name: String,
}

#[derive(Insertable)]
#[table_name = "users"]
pub struct NewUser<'a>{
    pub name: &'a str,
}


#[derive(Identifiable, Associations, Queryable, PartialEq, Debug)]
#[diesel(belongs_to(User))]
pub struct Post {
    pub id: i32,
    pub user_id: i32,
    pub title: String,
    pub body: String,
    pub published: bool,
}

#[derive(Insertable)]
#[table_name = "posts"]
pub struct NewPost<'a>{
    pub user_id: &'a i32,
    pub title: &'a str,
    pub body: &'a str,
    pub published: &'a bool,
}
No answers.You can refer to the related questions on the right.
10 No cache on rust-diesel

I am using MySQL as my database, and diesel to retrieve the data. The data get updated every second from multiple end-points. The problem is while usi ...

暂无
暂无

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-2022 STACKOOM.COM