SeaQL / sea-query

🔱 A dynamic SQL query builder for MySQL, Postgres and SQLite
https://www.sea-ql.org
Other
1.18k stars 195 forks source link

Cast issues with enums #667

Closed SteelAlloy closed 1 year ago

SteelAlloy commented 1 year ago

The following issue is a repost of https://github.com/SeaQL/sea-orm/issues/1527, since this issue actually belongs here.

I've also encountered the bug with gte : `"operator does not exist: some_enum >= text".


Description

.is_in(...) and .is_not_in(...) don't use CAST when comparing enums.

gte as well and other operators I guess.

Steps to Reproduce

  1. Create a table in Postgres, that has a field with custom type as enum.
  2. Make a query to that table, having a condition of this enum field IN vector.

Expected Behavior

The same as with .eq, .ne operators. Resulting query should look something like this.

-- For .eq operator, it generates correctly
WHERE "table"."enum_field" = CAST($1 AS enum_type)
-- For .is_in operator, it generates a different query without CAST
WHERE "table"."enum_field" IN (CAST($1 AS enum_type), CAST($2 AS enum_type))

Actual Behavior

Generated query looks something like this

WHERE "table"."enum_field" IN ($1, $2)

PgDatabaseError is returned.

{severity: Error, code: "42883", message: "operator does not exist: enum_type <> text", ...}

Reproduces How Often

Always, I assume. But the bug was spoted when in subquery.

Versions

sea-orm v0.11.0 postgres macos Ventura

sea-orm v0.12.1 postgres ubuntu 22

Additional Information

cc @0xAndoroid

tyt2y3 commented 1 year ago

I think this is a bug only in SeaORM, as all cast as is applied by the ORM. I don't think there is any limitation in SeaQuery.