Search code examples
sqliterustrust-diesel

Diesel ORM error: no method `eq` on type `ID`


I am trying to create a database to hold a table called 'MyTable', where I will upload a file to the database. I ran through the tutorial for diesel ORM linked here: http://diesel.rs/guides/getting-started.html I have an automatic generated schema for my table:

    diesel::table! {
        MyTable (ID) {
            ID -> Integer,
            ContentType -> Text,
            AttachmentName -> Text,
            AttachmentData -> Binary,
        }
    }

I ran into an issue where I it is throwing me an error like so:

no method `eq` on type `ID`rust-analyzerunresolved-method
no method `eq` on type `ContentType`rust-analyzerunresolved-method
no method `eq` on type `AttachmentName`rust-analyzerunresolved-method
no method `eq` on type `AttachmentData`rust-analyzerunresolved-method

This is my Cargo.toml:

    [package]
    name = "FileControl"
    version = "0.1.0"
    edition = "2021"
    default-run = "my-binary"

    [dependencies]
    Inflector = "0.11.4"
    ccase = "0.4.1"
    chrono = { version = "0.4", features = ["serde"] }
    diesel = { version = "2.0.0", features = ["sqlite","chrono"] }
    smartcore = {version = "0.3.0", features=["serde"]}
    dotenvy = "0.15"
    kv-derive = "1.0.1"
    proc-macro-error = "1.0.4"
    rocket = "0.4.11"
    serde = { version = "1.0", features = ["derive"] }
    serde_derive = "1.0.151"
    serde_json = "1.0"
    rocket_contrib = { version = "0.4.11", features = ["json"] }
    cargo-watch = "8.3.0"
    rustc-serialize = "0.3.24"
    base64 = "0.21.0"
    rocket-multipart-form-data = "0.10.5"
    uuid = { version = "1.3.0", features = ["v4"] }
    mime_guess = "2.0.4"
    dirs-2 = "3.0.1"


    [[bin]]
    name = "my-binary"
    path = "src/main.rs"

And this is an example of what I would be trying for:

    #[allow(non_snake_case)]
    pub fn establish_connection() -> SqliteConnection {
        dotenv().ok();
        let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
        SqliteConnection::establish(&database_url)
            .unwrap_or_else(|_| panic!("Error connecting to {}", database_url))
    }


    pub fn create_attachment(
        conn: &mut SqliteConnection,
        file: Vec<u8>,
        file_name: String,
        content_type: String,
    ) {
        let new_file = NewAttachment {
            AttachmentData: file,
            AttachmentName: file_name,
            ContentType: content_type,
        };
        diesel::insert_into(attachment_schema::table)
            .values(&new_file)
            .execute(conn)
            .expect("Error inserting data into database");
    }
    fn option_vec_to_slice(opt_vec: Option<Vec<u8>>) -> &'static [u8] {
        match opt_vec {
            Some(vec) => Box::leak(vec.into_boxed_slice()),
            None => &[],
        }
    }

    pub fn download_db_file(
        id: i32,
    ) -> io::Result<std::fs::File> {
        let connection = &mut establish_connection();
        let file_data = attachment_schema::table
            .filter(attachment_schema::ID.eq(id))
            .first::<attachment_model>(connection)
            .unwrap();
        let data = file_data.File;
        let file_name = file_data.FileName.unwrap();
        let download_dir = match dirs_2::download_dir() {
            Some(path) => path,
            None => {
                return Err(std::io::Error::new(
                    std::io::ErrorKind::NotFound,
                    "Download directory not found",
                ))
            }
        };
        let file_path = download_dir.join(&file_name);
        let mut file = std::fs::File::create(&file_path)?;
        let data_ref: &[u8] = option_vec_to_slice(data);
        file.write_all(data_ref)?;
        Ok(file)
    }

I have tried googling and using chatGPT to find the fix. I have deleted my Cargo.lock and re compiled, cargo clean, cargo update, and a numch of other methods. I have even deleted everthing and started all over again. And still have the same issue


Solution

  • Your question does only contain output from rust-analyzer. This tool is known to not being able to represent all valid rust code correctly. If your code compiles with rustc that's very likely another issue of that kind. You should fill an issue in the rust-analyzer repository in that case.