adwhit/diesel-derive-enum

`insert_into` with tuple -> `.eq` not found + can't derive insertable

Ten0 opened this issue · 4 comments

Ten0 commented

I'm trying to run an insert_into, one of the columns being a tuple that corresponds to an enum for which DbEnum and Eq are implemented.

Diesel complains that it can't create table::some_enum.eq expressions, because .eq does not exist on some_enum.

Here is the complete error:

error[E0599]: no method named `eq` found for type `schema::item_matching_keys::columns::type_` in the current scope
  --> /src/items/import.rs:76:33
   |
76 |                           item_matching_keys::type_.eq(mk.type_),
   |                                                     ^^
   | 
  ::: /src/schema.rs:45:1
   |
45 | / table! {
46 | |     use diesel::sql_types::*;
47 | |     use crate::schema_additions::MatchingKeyType;
48 | |     item_matching_keys (id) {
...  |
54 | |     }
55 | | }
   | |_- method `eq` not found for this
   |
   = note: the method `eq` exists but the following trait bounds were not satisfied:
           `&mut schema::item_matching_keys::columns::type_ : diesel::ExpressionMethods`
           `&mut schema::item_matching_keys::columns::type_ : std::iter::Iterator`
           `&schema::item_matching_keys::columns::type_ : diesel::ExpressionMethods`
           `schema::item_matching_keys::columns::type_ : diesel::ExpressionMethods`
   = help: items from traits can only be used if the trait is implemented and in scope
   = note: the following traits define an item `eq`, perhaps you need to implement one of them:
           candidate #1: `std::cmp::PartialEq`
           candidate #2: `std::iter::Iterator`
           candidate #3: `diesel::ExpressionMethods`
   = note: this error originates in a macro outside of the current crate (in Nightly builds, run with -Z external-macro-backtrace for more info)

It seems the generated column would need to implement Expression.
How can I insert my enum in database using tuples ?

Also, if I try to insert it using a struct, by having a structure that contains one such tuple derive Insertable, it complains when trying to derive that trait :

error[E0277]: the trait bound `schema_additions::MatchingKeyType: diesel::Expression` is not satisfied
 --> /src/items/matching_keys.rs:7:38
  |
7 | #[derive(Debug, PartialEq, Eq, Hash, Insertable)]
  |                                      ^^^^^^^^^^ the trait `diesel::Expression` is not implemented for `schema_additions::MatchingKeyType`
  |
  = note: required because of the requirements on the impl of `diesel::Expression` for `&'insert schema_additions::MatchingKeyType`
  = note: required because of the requirements on the impl of `diesel::expression::AsExpression<schema_additions::MatchingKeyType>` for `&'insert schema_additions::MatchingKeyType`

I can't insert my enum in database. Please help !

Interesting. Could you post some code so I can attempt to reproduce?

Ten0 commented

Yes, thank you!

I crated a minimal project where the issue appears:
https://github.com/Ten0/diesel-derive-enum-cantinsert

You can try and comment out try_insert_1 or try_insert_2 depending on the insert method you want to test.

I have noted that it seems to go a little further when adding SqlType to the derives of the schema::MatchingKeyType enum (along with DbEnum). I tried it because that was the trait that triggered the .eq method to be implemented on other types.

I think you need to change

	type_ -> MatchingKeyType

to

	type_ -> MatchingKeyTypeMapping

On this line: https://github.com/Ten0/diesel-derive-enum-cantinsert/blob/master/src/schema.rs#L14
You will also need to change line 10 to:

	  use super::MatchingKeyTypeMapping;

Read the "Type Renaming" section of the README for an explanation.

Ten0 commented

That fixed it indeed!
Thank you very much!