使用路径分隔符来引用项目

use the path separator to refer to an item

我想使用生锈的柴油过滤器方法添加这样的过滤条件:

#[macro_use]
extern crate diesel;

use crate::schema::dict::id;
mod schema;
mod models;

fn main() {
    use crate::diesel::prelude::*;
    use crate::schema::dict;
    let query = dict.filter(id.gt(0));
}

这是字典 table 方案定义:

table! {
dict (id) {
    word -> Varchar,
    phonetic -> Varchar,
    definition -> Varchar,
    translation -> Varchar,
    pos -> Varchar,
    collins -> Nullable<Int4>,
    oxford -> Nullable<Int4>,
    tag -> Varchar,
    bnc -> Int4,
    frq -> Int4,
    exchange -> Varchar,
    detail -> Varchar,
    audio -> Varchar,
    id -> Int8,
  }
}

这是模型定义:

use rocket::serde::Serialize;
use rocket::serde::Deserialize;
use crate::schema::dict;

#[derive(Insertable, Serialize, Queryable, Deserialize,Default)]
#[table_name = "dict"]
pub struct QueryEdict {
    pub id: i64,
    pub word: String,
    pub phonetic: String,
    pub definition: String,
    pub translation: String,
    pub pos: String,
    pub collins: Option<i32>,
    pub oxford: Option<i32>,
    pub tag: String,
    pub bnc: i32,
    pub frq: i32,
    pub exchange: String,
    pub detail: String,
    pub audio: String,
}

编译rust代码时显示错误:

   Compiling rust-learn v0.1.0 (/Users/xiaoqiangjiang/source/reddwarf/backend/rust-learn)
error[E0423]: expected value, found module `dict`
  --> src/main.rs:11:17
   |
11 |     let query = dict.filter(id.gt(0));
   |                 ^^^^-------
   |                 |
   |                 help: use the path separator to refer to an item: `dict::filter`
   |
note: unit struct `crate::models::dict::dsl::dict` exists but is inaccessible
  --> src/schema.rs:1:1
   |
1  | / table! {
2  | |     dict (id) {
3  | |         word -> Varchar,
4  | |         phonetic -> Varchar,
...  |
17 | |     }
18 | | }
   | |_^ not accessible
   = note: this error originates in the macro `__diesel_table_impl` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0423`.
error: could not compile `rust-learn` due to previous error

为什么不能使用柴油滤清器?我该怎么做才能解决它?这是 Cargo.toml 定义:

[package]
name = "rust-learn"
version = "0.1.0"
edition = "2018"

[dependencies]
rocket = { version = "0.5.0-rc.1", features = ["json"] }
serde = { version = "1.0.64", features = ["derive"] }
serde_json = "1.0.64"
serde_derive = "1.0"
# database
diesel = { version = "1.4.7", features = ["postgres"] }

这是系统信息:

您的错误消息清楚地表明了问题所在:

   Compiling rust-learn v0.1.0 (/Users/xiaoqiangjiang/source/reddwarf/backend/rust-learn)
error[E0423]: expected value, found module `dict`
  --> src/main.rs:11:17
   |
11 |     let query = dict.filter(id.gt(0));
   |                 ^^^^-------
   |                 |
   |                 help: use the path separator to refer to an item: `dict::filter`
   |
note: unit struct `crate::models::dict::dsl::dict` exists but is inaccessible
  --> src/schema.rs:1:1
   |
1  | / table! {
2  | |     dict (id) {
3  | |         word -> Varchar,
4  | |         phonetic -> Varchar,
...  |
17 | |     }
18 | | }
   | |_^ not accessible
   = note: this error originates in the macro `__diesel_table_impl` (in Nightly builds, run with -Z macro-backtrace for more info)

For more information about this error, try `rustc --explain E0423`.
error: could not compile `rust-learn` due to previous error

它声明范围内没有值 dict,只有一个具有该名称的模块。模块在值位置无效。此外,它还会提示您如何解决该问题:

note: unit struct `crate::models::dict::dsl::dict` exists but is inaccessible

这提示您有可用的同名单元结构,但未导入。导入此类型确实可以解决您的问题。

提供更多上下文:单元结构是类型定义 + 值合二为一。所以对于

struct UnitStruct;

标识符UnitStruct可用于值和类型位置。 Diesel 使用单元结构作为 table 和列名的标识符。查看 "Schema in depth" guide 了解详情。