Не найдена функция или связанный элемент с именем `table` для структуры `schema::todos::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,
    }
}

person BoKuToTsuZenU    schedule 06.11.2020    source источник
comment
Я очень подозреваю, что ваша таблица не называется table. Кроме того, вам нужен атрибут #[table_name = "tablename"] в модели.   -  person Ivan C    schedule 06.11.2020
comment
Попробуйте todos.load(...)   -  person Ibraheem Ahmed    schedule 06.11.2020


Ответы (1)


Я думаю, вы перепутали два разных способа обращения к конкретной таблице. Согласно соответствующему руководству, это либо crate::schema::table_name::table, либо crate::schema::table_name::dsl::table_name. Вы пытаетесь использовать crate::schema::table_name::dsl::table_name::table.

person weiznich    schedule 09.11.2020