[英]What needs to be imported to get the belonging_to function visible in a different scope?
I'm working through the Disel's getting started demo, but adding associations with a user who can post.我正在完成 Disel 的入门演示,但添加了与可以发布的用户的关联。 When I try to use the
belonging_to()
function it gives the following error:当我尝试使用 belongs_to
belonging_to()
function 时,会出现以下错误:
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
在当前
diesel_demo::models::Post
diesel_demo::models::Post
或相关项目中找不到belonging_to
或名为 belongs_to 的关联项
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.我在结构上使用了
Associations
宏,我认为这足以让 belongs_to belonging_to
工作,但我错过了一些东西。
Here is a simple query where I try to use belonging_to()
, but this fails.这是一个简单的查询,我尝试使用
belonging_to()
,但是失败了。
#[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.这是结构所在的 models.rs 文件。
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,
}
This is from cargo check
:这是来自
cargo check
:
error[E0599]: no function or associated item named `belonging_to` found for struct `diesel_demo::models::Post` in the current scope
--> src/bin/publish_post.rs:18:27
|
18 | let post_list = Post::belonging_to(&user)
| ^^^^^^^^^^^^ function or associated item not found in `diesel_demo::models::Post`
What you're looking for is the BelongingToDsl
trait, which is in diesel::query_dsl
or in the prelude.您正在寻找的是
BelongingToDsl
特征,它位于diesel::query_dsl
或前奏中。
You already have that imported into scope from use diesel::prelude::*
.您已经通过
use diesel::prelude::*
将其导入 scope 。 The reason you aren't seeing the implementation is because you've mismatched 1.0-style and 2.0-style attributes.您没有看到实现的原因是因为您不匹配 1.0 样式和 2.0 样式的属性。
If you're using Diesel 1.0, then your attributes should look like this ( docs ):如果您使用的是 Diesel 1.0,那么您的属性应如下所示(文档):
#[derive(Identifiable, Associations, Queryable, PartialEq, Debug)]
#[belongs_to(User)]
#[table_name = "posts"]
pub struct Post {
pub id: i32,
pub user_id: i32,
...
But if you're using Diesel 2.0, they should look like this ( docs ):但如果您使用的是 Diesel 2.0,它们应该看起来像这样(文档):
#[derive(Identifiable, Associations, Queryable, PartialEq, Debug)]
#[diesel(belongs_to(User))]
#[diesel(table_name = posts)]
pub struct Post {
pub id: i32,
pub user_id: i32,
...
Using the correct one should resolve the problem.使用正确的应该可以解决问题。 I'm surprised that the invalid attributes don't cause an error during code expansion, but I'll let that be a mystery for another day.
我很惊讶无效属性在代码扩展期间不会导致错误,但我会让它成为另一天的谜。
问题未解决?试试以下方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.