-
Consider the following implementation: schema.rs diesel::table! {
use diesel::sql_types::*;
sample_entities (id) {
id -> Uuid,
contents -> Bytea,
}
} sample.rs use crate::schema::*;
use diesel::prelude::*;
use diesel::query_builder::QueryId;
use diesel::{AsChangeset, Identifiable, Insertable, Queryable};
use uuid::Uuid;
#[derive(Debug, Clone, Identifiable, Queryable, Insertable, AsChangeset)]
#[diesel(table_name = sample_entities)]
#[diesel(primary_key(id))]
pub struct SampleEntity {
pub id: Uuid,
pub contents: Contents,
}
use bytes::Bytes;
use diesel::deserialize::FromSql;
use diesel::expression::AsExpression;
use diesel::serialize::{Output, ToSql};
use diesel::sql_types::Bytea;
use diesel::FromSqlRow;
#[derive(Default, Debug, Clone, PartialEq, Eq, Hash, FromSqlRow, AsExpression)]
#[diesel(sql_type = diesel::sql_types::Bytea)]
pub struct Contents(Bytes);
impl From<Vec<u8>> for Contents {
fn from(value: Vec<u8>) -> Self {
Contents {
0: Bytes::from(value),
}
}
}
impl From<serde_json::Value> for Contents {
fn from(value: serde_json::Value) -> Self {
Contents {
0: Bytes::from(value.to_string()),
}
}
}
impl<DB: diesel::backend::Backend> ToSql<Bytea, DB> for Contents
where
Bytes: ToSql<Bytea, DB>,
{
fn to_sql<'b>(&'b self, out: &mut Output<'b, '_, DB>) -> diesel::serialize::Result {
self.0.to_sql(out)
}
}
impl<DB: diesel::backend::Backend> FromSql<Bytea, DB> for Contents
where
Bytes: FromSql<Bytea, DB>,
{
fn from_sql(bytes: DB::RawValue<'_>) -> diesel::deserialize::Result<Self> {
let bytes = Bytes::from_sql(bytes)?;
Ok(Contents { 0: bytes })
}
} main.rs use std::env;
use diesel::associations::HasTable;
use diesel_async::{AsyncConnection, AsyncPgConnection, RunQueryDsl};
use crate::types::sample::SampleEntity;
#[tokio::main]
async fn main() {
let database_url = env::var("DATABASE_URL").expect("DATABASE_URL must be set");
let mut conn = AsyncPgConnection::establish(&database_url).await.unwrap();
SampleEntity::table()
.get_results::<SampleEntity>(&mut conn)
.await
.unwrap();
} When trying to run main.rs, I get the following compilation error:
How can I go about fixing this error? Curiously, this only seems to occur if |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
The error message already contains the first hint on how to fix that problem:
Doing that gives you the following error message:
If you look at the documentation of You can make this work by changing the bounds in your |
Beta Was this translation helpful? Give feedback.
The error message already contains the first hint on how to fix that problem:
Doing that gives you the following error message: