简体   繁体   English

需要导入什么才能在不同的 scope 中看到属于 function 的内容?

[英]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 struct diesel_demo::models::Post in the current scope function or associated item not found in diesel_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.

相关问题 当使用与 tokio-diesel 关联时,“argument 要求 `record` 是为 `'static` 借用的” - “argument requires that `record` is borrowed for `'static`” when using belonging_to associations with tokio-diesel 即使 object 模块实现了特征,也需要在当前 scope 中导入特征 - Trait needs to be imported in current scope even though object module implement the trait 我该遵循哪个函数生命周期 &#39;a 或仅作用域生命周期? - What do I follow Which function lifetime 'a or just scope lifetime? 未命名值的范围是什么? - What is the scope of unnamed values? 在此 scope 中找不到 function 在此 scope 中未找到 ZF5E265D607CB720858FC166E8Z - cannot find function in this scope not found in this scope in Rust 从 Rust 中的 function 返回值的不同方法是什么? - What are the different ways to return a value from a function in Rust? 当作用域中的变量超出作用域时,是否会将它们复制到其他内存位置? - Are variables in a scope copied to a different memory location when they go beyond the scope? Rust 的 static 变量的 scope 是什么? - What is the scope of Rust's static variables? 属于特征的对象的向量 - Vector of objects belonging to a trait 当我从另一个模块使用结构时导入什么? - What is imported when I use a struct from another module?
 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM