如何解决在当前范围内未找到针对struct`schema :: todos :: table`的名为table的函数或相关项
我想创建一个函数,该函数使用Diesel读取postgresql中的所有数据,并将其作为Vec
返回。
错误消息
我相信此错误消息表明todos
未实现table
函数。我认为这不是问题,因为模型中有#[derive(Queryable)]
。
error[E0599]: no function or associated item named `table` found for struct `schema::todos::table` in the current scope
--> src/actions.rs:9:42
|
9 | let entries: Vec<TodoEntry> = todos::table.load::<TodoEntry>(&connection).expect("Error loading todos");
| ^^^^^ function or associated item not found in `schema::todos::table`
|
::: src/schema.rs:1:1
|
1 | / table! {
2 | | todos (id) {
3 | | id -> Int4,4 | | body -> Text,5 | | }
6 | | }
| |_- function or associated item `table` not found for this
|
= help: items from traits can only be used if the trait is in scope
= note: the following trait is implemented but not in scope; perhaps add a `use` for it:
`use crate::diesel::associations::HasTable;`
相关代码
Cargo.toml
[package]
authors = ["hogehogehoge"]
edition = "2018"
name = "todo"
version = "0.1.0"
[dependencies]
actix-web = "3"
askama = "*"
thiserror = "*"
diesel = { version = "1.4.4",features = ["postgres","r2d2"] }
r2d2 = "0.8"
dotenv = "0.15.0"
diesel.toml
[print_schema]
file = "src/schema.rs"
actions.rs
此文件具有一个函数,该函数以Vec
的形式返回在数据库中注册的数据。
use diesel::pg::PgConnection;
use crate::models;
use crate::templates::TodoEntry;
pub fn return_all(connection: &PgConnection) -> Result<Vec<TodoEntry>,diesel::result::Error> {
use crate::schema::todos::dsl::*;
let entries: Vec<TodoEntry> = todos::table.load::<TodoEntry>(&connection).expect("Error loading todos");
Ok(entries)
}
templates.rs
#[derive(Queryable)]
pub struct Todo {
pub id: u32,pub text: String,}
schema.rs
table! {
todos (id) {
id -> Int4,body -> Text,}
}
解决方法
我认为您已经混合使用两种不同的方式来引用特定的表。根据{{3}},它是crate::schema::table_name::table
或crate::schema::table_name::dsl::table_name
。您尝试使用crate::schema::table_name::dsl::table_name::table
。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。