RobinBlomberg / kysely-codegen

Generate Kysely type definitions from your database.
MIT License
771 stars 69 forks source link

Mismatch between Kysely return type and generated type #89

Closed jasonpanosso closed 6 months ago

jasonpanosso commented 1 year ago

While doing a simple select on a view, Kysely seems to infer the type of one of the returned columns as productCount: string | null;

kysely-codegen generated the type of this column as: productCount: Int8 | null

This is the raw SQL for the column of the view in question: count(p.id) AS product_count p.id is the primary key of a table products, defined as: GENERATED ALWAYS AS IDENTITY

Appreciate any advice, or a pointer on how to handle it correctly if this is intended behavior. Thanks for the tool, it works great otherwise!

Upvote & Fund

Fund with Polar

BoscoDomingo commented 9 months ago

I believe this is intended behaviour? I don't see how a count() could be anything but an int or NULL. If what you want is for Kysely to detect it as an int instead of string, make sure you're passing the type to the query, or explicitly mark it as so. If you give more context I could give a better answer

koskimas commented 6 months ago

The type of count(expression) doesn't depend on the expression. Why would the type that expresses the count of something depend on the type of the things you're counting?

The type is always a DB-dependent integer type, usually an 8bit integer. Since Kysely types are not dialect dependent, you need to manually use the correct type count<number>('foo.bar').