'use the path separator to refer to an item
I want do using rust diesel filter method to add a filter condition like this:
#[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));
}
and this is the dict table scheme define:
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,
}
}
this is the model define:
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,
}
show error when compile the rust code:
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
why could not using the diesel filter? what should I do to fix it? This is the Cargo.toml define:
[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"] }
this is the system info:
- rustc 1.57.0 (f1edd0429 2021-11-29)
- diesel = { version = "1.4.7", features = ["postgres","64-column-tables","chrono"] }
- Operation System: macOS Monterey 12.3
- Databse: PostgreSQL 13
Solution 1:[1]
Your error message clearly show the problem:
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
It states that there is no value dict in scope, only a module with that name. A module is not valid in value position. Additionally it gives you a hint how to fix that:
note: unit struct `crate::models::dict::dsl::dict` exists but is inaccessible
This gives you the hint that there is a unit struct with the same name available, but it is not imported. Importing this type does fix your problem.
To give a bit more context: Unit structs are type definitions + value in one. So for
struct UnitStruct;
the identifier UnitStruct can be used in value and in type position. Diesel uses unit structs as identifier for table and column names. Checkout the "Schema in depth" guide for details.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | weiznich |
