adwhit/diesel-derive-enum

Missing `Clone` implementation on generated struct

Closed this issue · 1 comments

The generated structs do not implement neither Clone (nor Copy, though it does not seem to be useful), which is an issue when building complex queries. I hit this problem while trying to use this in the on clause for a left_join, and though I didn't test, most likely for a inner_join.

Example

table! {
    user (id) {
        id -> Uuid,
    }
}
table! {
    use diesel::sql_types::*;
    use super::Server_status;
    server (id) {
        id -> Uuid,
        user_id -> Uuid,
        status -> Server_status,
    }
}

joinable!(server -> user (user_id));
allow_tables_to_appear_in_same_query!(user, server);

#[derive(DbEnum, Clone)]
#[DieselType = "Server_status"]
enum ServerStatus { Stopped, Starting, Running, Stopping, Stopped }

#[derive(Queryable)]
#[table_name = "user"]
struct User {
    id: Uuid,
}

#[derive(Queryable, Associations)]
#[belongs_to(User)]
#[table_name = "server"]
struct Server {
    id: Uuid,
    user_id: Uuid,
    status: ServerStatus,
}

let user_and_server = user
    .find(Uuid::new_v4())
    .left_join(
        server
            .on(server::dsl::user_id.eq(user::dsl::id.nullable())
                .and(server::dsl::status.eq(ServerStatus::Running))
        )
    )
    .first::<(User, Option<Server>)>(&conn)?;

The above wouldn't compile, complaining that Server_status does not implement Clone:

error[E0277]: the trait bound `Instance_status: Clone` is not satisfied
   --> src/main.rs:346:13
    |
346 | /             server
347 | |                 .on(server::dsl::user_id.eq(user::dsl::id.nullable())
348 | |                     .and(server::dsl::status.eq(ServerStatus::Running))),
    | |_______________________________________________________________________^ expected an implementor of trait `Clone`
    |
    = note: required because of the requirements on the impl of `Clone` for `diesel::expression::bound::Bound<Server_status, ServerStatus>`
...
(walls of trait bounds)

Adding the following made the code happily compile and run:

impl Clone for Server_status {
    fn clone(&self) -> Self { Self }
}

Thanks for the great lib!

Thanks for the excellent bug report. This has been fixed in release 1.1.1.