RobinBlomberg / kysely-codegen

Generate Kysely type definitions from your database.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Mismatch between Kysely return type and generated type

jasonpanosso opened this issue · comments

commented

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

  • We're using Polar.sh so you can upvote and help fund this issue.
  • We receive the funding once the issue is completed & confirmed by you.
  • Thank you in advance for helping prioritize & fund our backlog.
Fund with Polar

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

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').