Rust, Diesel and Enum

Hello,
I'm trying to make a small application with Rust, SQLite and Diesel.
I'm stuck using an Enum in my model and schema.

My Cargo.toml file :

[dependencies]
diesel = { version = "1.4", features = ["sqlite",  "chrono", "uuidv07"] }
dotenv = "0.15.0"
uuid = { version = "0.8", features = ["serde", "v4"] }
serde = "1.0"
serde_json = "1.0"
serde_derive = "1.0"
chrono = "0.4"

My model file :

use super::schema::todos;

#[derive(Debug)]
pub enum _Priority {
    _Low,
    _Medium,
    _High,
}

#[derive(Queryable, Identifiable, Debug)]
pub struct Todo {
    pub id: String,
    pub title: String,
    pub priority: i32,
    pub insert_date: chrono::NaiveDate,
    pub due_date: Option<chrono::NaiveDate>,
    pub is_done: bool,
}

#[derive(Insertable)]
#[table_name = "todos"]
pub struct NewTodo<'a> {
    pub id: &'a str,
    pub title: &'a str,
    pub priority: &'a i32,
    pub insert_date: &'a chrono::NaiveDate,
    pub due_date: Option<&'a chrono::NaiveDate>,
}

And my schema file

table! {
    todos (id) {
        id -> Varchar,
        title -> Varchar,
        priority -> Int4,
        insert_date -> Date,
        due_date -> Nullable<Date>,
        is_done -> Bool,
    }
}

I'm trying to replace the "priority" field with the enum of the same name. I know how to do it in my model but for the schema I can't find anything like information. can you help me ?

I'm pretty sure you should just keep using Int4 in the schema and use your enum only in the structs.

Thank you. Now I have other error

use super::schema::todos;

#[derive(Debug)]
pub enum Priority {
    Low,
    Medium,
    High,
}

#[derive(Queryable, Identifiable, Debug)]
pub struct Todo {
    pub id: String,
    pub title: String,
    pub priority: Priority,
    pub insert_date: chrono::NaiveDate,
    pub due_date: Option<chrono::NaiveDate>,
    pub is_done: bool,
}
#[derive(Insertable)]
#[table_name = "todos"]
pub struct NewTodo<'a> {
    pub id: &'a str,
    pub title: &'a str,
    pub priority: &'a Priority,
    pub insert_date: &'a chrono::NaiveDate,
    pub due_date: Option<&'a chrono::NaiveDate>,
}

table! {
todos (id) {
id -> Varchar,
title -> Varchar,
priority -> Varchar,
insert_date -> Date,
due_date -> Nullable,
is_done -> Bool,
}
}
``

I have put a Varchar and not a Int4....the enum is a string. But if I try with Int4 I have the same error.
In the model :

the trait bound `db::models::Priority: diesel::Expression` is not satisfied

the trait `diesel::Expression` is not implemented for `db::models::Priority`

note: required because of the requirements on the impl of `diesel::Expression` for `&'a db::models::Priority`
note: required because of the requirements on the impl of `diesel::expression::AsExpression<diesel::sql_types::Text>` for `&'a db::models::Priority`rustc(E0277)

And when I try to load or insert a record :

the trait bound `db::models::Priority: diesel::Expression` is not satisfied

the trait `diesel::Expression` is not implemented for `db::models::Priority`

note: required because of the requirements on the impl of `diesel::Expression` for `&db::models::Priority`rustc(E0277)
db.rs(48, 10): the trait `diesel::Expression` is not implemented for `db::models::Priority`

The fonction is :

pub fn get_todos() -> Vec<Todo> {
    let connection = establish_connection();
    todos
        .limit(5)
        .load::<Todo>(&connection)
        .expect("Error loading posts")
}

Check out this example: https://github.com/diesel-rs/diesel/blob/master/diesel_tests/tests/custom_types.rs

Depending on which diesel version you are using you may want to use the corresponding branch on the 1.4.x branch: https://github.com/diesel-rs/diesel/blob/1.4.x/diesel_tests/tests/custom_types.rs

That code is compatible with the latest release, while the master branch contains quite a few breaking changes.

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.