Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Experiencing a "CompatibleType<Attachment, Sqlite> not implemented" error when filtering by ID on a passed-in connection

I am trying to create a sqlite database where I can upload a file in bytes. I have an issue with the connection being passed in when I try to filter by ID.

This is my error:

[E0277] the trait bound `(diesel::sql_types::Integer, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Binary): load_dsl::private::CompatibleType<Attachment, Sqlite>` is not satisfied. 
[Note] the trait `load_dsl::private::CompatibleType<Attachment, Sqlite>` is not implemented for `(diesel::sql_types::Integer, diesel::sql_types::Text, diesel::sql_types::Text, diesel::sql_types::Binary)`

This is my schema:

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

These are my models:

use diesel;
use diesel::{Insertable, Queryable};
use serde::*;
use crate::schema::MyTable as file_attachment;

#[derive(Queryable)]
pub struct Attachment {
    pub ID: i32,
    pub AttachmentData: Vec<u8>,
    pub AttachmentName: String,
    pub ContentType: String,
}

#[derive(Insertable, Serialize)]
#[diesel(table_name=file_attachment)]
pub struct NewAttachment {
    pub AttachmentData: Vec<u8>,
    pub AttachmentName: String,
    pub ContentType: String,
}

And this is my code where I am getting the error, note: the error comes on function download_db_file:

use diesel::connection::Connection;
use diesel::prelude::*;
use diesel::sqlite::SqliteConnection;
use dotenvy::dotenv;
use serde_json::Value;
use std::env;
use std::io;
use std::io::Write;

use self::models::NewAttachment;
use self::models::Attachment as attachment_model;
use self::schema::MyTable as attachment_schema;


pub mod models;
pub mod schema;

#[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_db_release_activity_task_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) //This is where the error occurs on "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)
}

As you see, my models are public, there is no variance between the types of my models compared to the schema. Are there any suggestions on how I can bypass this error?

like image 348
Stefan Hall Avatar asked Nov 27 '25 19:11

Stefan Hall


1 Answers

The fix to this was the order of the model. I had the model out of order.

like image 192
Stefan Hall Avatar answered Dec 01 '25 23:12

Stefan Hall