I am attempting to use Tokio-Diesel to use async/await with my ORM. I am not able to use Diesel's belonging_to associations, as it complains that I ne ...
提示:本站收集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 structdiesel_demo::models::Post
in the current scope function or associated item not found indiesel_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,
}
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.