go-jet / jet

Type safe SQL builder with code generation and automatic query result data mapping
Apache License 2.0
2.23k stars 110 forks source link

Auto type Casting is wrong for custom type #260

Closed kislenko-artem closed 10 months ago

kislenko-artem commented 11 months ago

Little description:

I have table storage, storage has column f_size (type fsize), but method EQ support only string, when sql-query bulding, I got cast-error because library trying custom type compare with String.

``


table.Storage.
SELECT(table.Storage.AllColumns).
WHERE(
  table.Storage.FSize.EQ(postgres.String("original")).
  AND(
     table.Storage.CreatedBy.EQ(postgres.UUID(u.ID)).OR(table.Storage.CreatedBy.IS_NULL())
  )
)

Query (builed by library):

sqlStatment 
SELECT storage.id AS "storage.id",
     storage.created_at AS "storage.created_at",
     storage.name AS "storage.name",
     storage.path AS "storage.path",
     storage.f_type AS "storage.f_type",
     storage.parent_id AS "storage.parent_id",
     storage.f_size AS "storage.f_size",
     storage.created_by AS "storage.created_by"
FROM public.storage
WHERE (storage.f_size = 'original'::text) AND (
 (storage.created_by = '50cb0a24-0000-4c8c-93ad-4137c4087662') OR storage.created_by IS NULL);

Error:

 jet: pq: operator does not exist: public.fsize = text

So error in this line: storage.f_size = 'original'::text

houten11 commented 11 months ago

For custom types jet generator assumes they are string types, so you'll need to customize the generator - https://github.com/go-jet/jet/wiki/Generator#generator-customization.