diesel-rs / diesel

A safe, extensible ORM and Query Builder for Rust
https://diesel.rs
Apache License 2.0
12.73k stars 1.07k forks source link

diesel::expression_methods::TextOrNullableText seems to be missing for diesel::sql_types::Uuid #2693

Closed Roms1383 closed 3 years ago

Roms1383 commented 3 years ago

Hello, thanks for this wonderful library !

I ran this morning into an issue when trying to use like on table fields of type diesel::sql_types::Uuid.

Setup

Versions

Feature Flags

Problem Description

When trying to use like or ilike (for Postgres) with a diesel table field of type Uuid, I run into the missing implementation of diesel::expression_methods::TextOrNullableText for diesel::sql_types::Uuid.

What are you trying to accomplish?

Just using LIKE filter on a query.

What is the expected output?

It should be able to filter diesel::sql_types::Uuid with LIKE.

What is the actual output?

Screen Shot 2564-03-30 at 11 34 06

Compilation error as shown above.

Are you seeing any additional errors?

None.

Steps to reproduce

# Cargo.toml
[dependencies]
diesel = { version = "1.4.5", features = ["postgres", "chrono", "r2d2", "uuidv07", "numeric"] }
uuid = { version = "0.8", features = ["serde", "v4"] }
// schema.rs
table! {
    use diesel::sql_types::*;
    my_table (id) {
        id -> Uuid,
    }
}
// querying like :
let mut query = my_table::table.into_boxed::<diesel::pg::Pg>();
query = query.filter(my_table::id.ilike("78621%"));
// or : query = query.filter(my_table::id.like("78621%"));
let results = query.load(&connection)?;

Checklist

Thanks :)

Mingun commented 3 years ago
diesel = { ... features = [..., "uuidv07", ...] }
uuid = { version = "0.8", ... }

Hm...

Roms1383 commented 3 years ago

Yes @Mingun that's what I have already (please check the Cargo.toml excerpt).

Roms1383 commented 3 years ago

@Mingun or do you mean remove the uuid dependency ?

Mingun commented 3 years ago

I mean that you probably need to use "uuid" feature of the diesel crate or use uuid with version <=0.7.

There are several closed issues in this repo with similar "trait not implemented" errors that connected with incompatible versions of dependencies

Roms1383 commented 3 years ago

well if I switch to :

[dependencies]
diesel = { version = "1.4.5", features = ["postgres", "chrono", "r2d2", "uuidv07", "numeric"] } # <-- using uuid feature as before
uuid = { version = "0.6", features = ["serde", "v4"] } # <-- with 0.6 version here

The problem is then I end up with compilation errors like :

Screen Shot 2564-03-30 at 14 10 37

And :

Screen Shot 2564-03-30 at 14 13 20

I have also tried in that different setup :

diesel = { version = "1.4.6", features = ["postgres", "chrono", "r2d2", "uuid", "numeric"] }
uuid = { version = "0.8.2", features = ["serde", "v4"] }

By switching uuid's to_hyphenated() method to hyphenated() but then running into these compilation errors :

Screen Shot 2564-03-30 at 14 24 55

Or even :

diesel = { version = "1.4.6", features = ["postgres", "chrono", "r2d2", "uuid", "numeric"] }
uuid = { version = "0.6.5", features = ["serde", "v4"] }

But no matter which I'm still running into these compilation errors :

Screen Shot 2564-03-30 at 14 22 58

So I must admit that I'm a little bit confused here @Mingun.

Roms1383 commented 3 years ago

@Mingun wouldn't it make sense to simply add :

impl TextOrNullableText for Uuid {}
impl TextOrNullableText for Nullable<Uuid> {}

to diesel::expression_methods::text_expression_methods ?

weiznich commented 3 years ago

@Roms1383 This error is expected as this would not generate valid SQL. Postgres does only support LIKE and similar operations on string types (so Text, VarChar and similar types). A expression of the type Uuid is not supported as left hand side of a LIKE expression. See the postgres documentation for details.

Closed as everything works as expected, which means in this case we turned a runtime error in your SQL query into a compile time error.

@Mingun That's totally unrelated to the uuid version and so on

Roms1383 commented 3 years ago

@Mingun Ok, is there another way to go to implement a LIKE or ILIKE search on a partial string representation of Uuid parameter ? e.g. ... WHERE id LIKE '2127%'

weiznich commented 3 years ago

@Roms1383 To make that clear: @Mingun Is not related to the diesel project in any way. They are just a person that commented on this issue trying to help you. Otherwise our issue tracker is not the right place to ask questions please use any of the other support variants for this.

Roms1383 commented 3 years ago

Ok thanks @weiznich I'm gonna look into it