RobinBlomberg / kysely-codegen

Generate Kysely type definitions from your database.
MIT License
819 stars 73 forks source link

Generates number for auto-increment id types instead of bigint #118

Open clemmy opened 1 year ago

clemmy commented 1 year ago

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

Fund with Polar

koskimas commented 10 months ago

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

gilbert commented 6 months ago

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