Skip to content

Instantly share code, notes, and snippets.

@l-7-l
Created March 12, 2021 20:54
Show Gist options
  • Save l-7-l/69c45e489ff18a8e508c7745a5a16ee0 to your computer and use it in GitHub Desktop.
Save l-7-l/69c45e489ff18a8e508c7745a5a16ee0 to your computer and use it in GitHub Desktop.
how to use postgres enum with diesel?

the trait bound sql_enums::Status: diesel::Expression is not satisfied required because of the requirements on the impl of diesel::expression::AsExpression<sql_enums::Status> for sql_enums::StatusrustcE0277 models.rs(19, 17): Exact error occurred here

the trait bound sql_enums::Status: diesel::Expression is not satisfied required because of the requirements on the impl of diesel::Expression for &'insert sql_enums::Status required because of the requirements on the impl of diesel::expression::AsExpression<sql_enums::Status> for &'insert sql_enums::StatusrustcE0277 models.rs(19, 17): Exact error occurred here

the trait bound sql_enums::Status: diesel::Expression is not satisfied required because of the requirements on the impl of diesel::Expression for &sql_enums::Status required because of the requirements on the impl of diesel::expression::AsExpression<sql_enums::Status> for &sql_enums::StatusrustcE0277 models.rs(19, 17): Exact error occurred here

use crate::schema::categories;
use sql_type::Status;
#[derive(Debug, Insertable)] // ⬅️ Insertable: An error occurred at this position ⬇️
#[table_name = "categories"]
pub struct NewCategory {
pub parent_id: Option<i64>,
pub is_leaf: bool,
pub level: i16,
pub status: Status,
pub name: String,
pub code: i64,
pub weight: i64,
}
// Error Info:
// >the trait bound `sql_enums::Status: diesel::Expression` is not satisfied
// required because of the requirements on the impl of `diesel::expression::AsExpression<sql_enums::Status>` for `sql_enums::Status`rustcE0277
// models.rs(19, 17): Exact error occurred here
// >the trait bound `sql_enums::Status: diesel::Expression` is not satisfied
// required because of the requirements on the impl of `diesel::Expression` for `&'insert sql_enums::Status`
// required because of the requirements on the impl of `diesel::expression::AsExpression<sql_enums::Status>` for `&'insert sql_enums::Status`rustcE0277
// models.rs(19, 17): Exact error occurred here
// > the trait bound `sql_enums::Status: diesel::Expression` is not satisfied
// required because of the requirements on the impl of `diesel::Expression` for `&sql_enums::Status`
// required because of the requirements on the impl of `diesel::expression::AsExpression<sql_enums::Status>` for `&sql_enums::Status`rustcE0277
// models.rs(19, 17): Exact error occurred here
#[derive(Debug, AsChangeset)] // ⬅️ Insertable: An error occurred at this position ⬇️
#[table_name = "categories"]
pub struct UpdateCategory {
pub parent_id: Option<i64>,
pub is_leaf: Option<bool>,
pub level: Option<i16>,
pub status: Option<Status>,
pub name: Option<String>,
pub weight: Option<i64>,
}
// Error info:
// the trait bound `sql_enums::Status: diesel::AppearsOnTable<schema::categories::table>` is not satisfied
// required because of the requirements on the impl of `diesel::AppearsOnTable<schema::categories::table>` for `&'update sql_enums::Status`
// required because of the requirements on the impl of `diesel::query_builder::AsChangeset` for `diesel::expression::operators::Eq<schema::categories::columns::status, &'update sql_enums::Status>`rustcE0277
// models.rs(31, 17): Exact error occurred here
// the trait bound `sql_enums::Status: diesel::AppearsOnTable<schema::categories::table>` is not satisfied
// required because of the requirements on the impl of `diesel::AppearsOnTable<schema::categories::table>` for `&sql_enums::Status`
// required because of the requirements on the impl of `diesel::query_builder::AsChangeset` for `diesel::expression::operators::Eq<schema::categories::columns::status, &sql_enums::Status>`rustcE0277
// models.rs(31, 17): Exact error occurred here
// the trait bound `sql_enums::Status: diesel::AppearsOnTable<schema::categories::table>` is not satisfied
// required because of the requirements on the impl of `diesel::query_builder::AsChangeset` for `diesel::expression::operators::Eq<schema::categories::columns::status, sql_enums::Status>`rustcE0277
// models.rs(31, 17): Exact error occurred here
table! {
use diesel::sql_types::*;
use crate::sql_enums::*;
categories (id) {
id -> Int8,
parent_id -> Nullable<Int8>,
is_leaf -> Bool,
level -> Int2,
status -> Status,
name -> Varchar,
code -> Int8,
weight -> Int8,
created_at -> Timestamptz,
updated_at -> Timestamptz,
}
}
use diesel::backend::Backend;
use diesel::deserialize::{self, FromSql};
use diesel::pg::Pg;
use diesel::serialize::{self, IsNull, Output, ToSql};
use diesel_derive_enum::DbEnum;
use std::io::Write;
#[derive(SqlType)]
#[postgres(type_name = "Status")]
pub struct StatusType;
#[derive(Debug, AsExpression, FromSqlRow)]
#[sql_type = "StatusType"]
pub enum Status {
Pending,
Processing,
Passed,
Unpassed,
Disabled,
Deleted,
Working,
Resting,
}
impl ToSql<StatusType, Pg> for Status {
fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
let s = match *self {
Status::Pending => out.write_all(b"pending")?,
Status::Processing => out.write_all(b"processing")?,
Status::Passed => out.write_all(b"passed")?,
Status::Unpassed => out.write_all(b"unpassed")?,
Status::Disabled => out.write_all(b"disabled")?,
Status::Deleted => out.write_all(b"deleted")?,
Status::Working => out.write_all(b"working")?,
Status::Resting => out.write_all(b"resting")?,
};
<&str as ToSql<StatusType, Pg>>::to_sql(s, out)
// Ok(IsNull::No)
}
}
// pub trait FromSql<A, DB: Backend>: Sized {
/// See the trait documentation.
/// at the diesel 1.4.4, in current master that is changed to diesel::pg::PgValue wrapper
/// fn from_sql(bytes: Option<&DB::RawValue>) -> Result<Self>;
///}
impl FromSql<StatusType, Pg> for Status {
fn from_sql(
bytes: Option<&<Pg as Backend>::RawValue>,
) -> deserialize::Result<Self> {
match bytes.as_bytes() {
b"pending" => Ok(Status::Pending),
b"processing" => Ok(Status::Processing),
b"passed" => Ok(Status::Passed),
b"unpassed" => Ok(Status::Unpassed),
b"disabled" => Ok(Status::Disabled),
b"deleted" => Ok(Status::Deleted),
b"working" => Ok(Status::Working),
b"resting" => Ok(Status::Resting),
_ => Err("Unrecognized enum variant".into()),
}
}
}
pub mod exports {
pub use super::StatusType as Status;
}
@l-7-l
Copy link
Author

l-7-l commented Mar 12, 2021

rust version:

❯ rustc -V
rustc 1.50.0 (cb75ad5db 2021-02-10)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment