RobinBlomberg / kysely-codegen

Generate Kysely type definitions from your database.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Generates number for auto-increment id types instead of bigint

clemmy opened this issue · comments

Related to an issue that was posted in the original kysely repo.

With a field that looks something like:

+------------+-----------------+------+-----+-------------------+-----------------------------------------------+
| Field      | Type            | Null | Key | Default           | Extra                                         |
+------------+-----------------+------+-----+-------------------+-----------------------------------------------+
| id         | bigint unsigned | NO   | PRI | NULL              | auto_increment                                |

The generated output is

id: Generated<number>;

I'd expect Generated<bigint> in this scenario to match typings in kysely, which wrap id with bigint.

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

Kysely doesn't wrap anything to bigint or any other type for that matter https://kysely.dev/docs/recipes/data-types.

The issue you linked refers to the default InsertResult type that's returned if you don't use the returning clause in your query. InsertType is static and never changes based on your types (the generated types).

To elaborate on @koskimas's answer, instead of writing:

const result = await this.db.insertInto('users').values(attrs).executeTakeFirst()
return result.insertId!

You want to write:

const result = await this.db.insertInto('users').values(attrs).returning('id').executeTakeFirst()
return result!.id