【问题标题】:use the path separator to refer to an item使用路径分隔符来引用一个项目
【发布时间】:2022-12-10 05:30:50
【问题描述】:

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

#[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! {
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"] }

这是系统信息:

  • rustc 1.57.0 (f1edd0429 2021-11-29)
  • diesel = { version = "1.4.7", features = ["postgres","64-column-tables","chrono"] }
  • 操作系统:macOS Monterey 12.3
  • 数据库:PostgreSQL 13

【问题讨论】:

    标签: rust rust-diesel


    【解决方案1】:

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

       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 使用单元结构作为表名和列名的标识符。查看"Schema in depth" guide了解详情。

    【讨论】:

      猜你喜欢
      • 2013-02-23
      • 1970-01-01
      • 2014-07-29
      • 2015-11-22
      • 2021-10-11
      • 1970-01-01
      • 2021-04-05
      • 2018-04-25
      • 2011-06-03
      相关资源
      最近更新 更多