valtyr / prisma-kysely

🪄 Generate Kysely types directly from your Prisma schema!
https://www.npmjs.com/package/prisma-kysely
MIT License
951 stars 36 forks source link

Handle enum primary keys on many-to-many relationships #100

Open frannyfx opened 6 months ago

frannyfx commented 6 months ago

This PR aims to fix an error that occurs when enums are used as part of implicit many-to-many relations.

Reproducing

enum RoleType {
  User
  Admin
}

model Role {
  roleId      RoleType     @id
  permissions Permission[]
}

model Permission {
  permissionId String @id
  roles        Role[]
}
yarn prisma generate

Cause

The generator for many-to-many implicit relationships presumes the fields involved in these relations are scalars, but they can also be enums too. The pre-supposition that the field is a scalar means that enums are not considered, and when converting from a Prisma type to a database type in generateFieldType, it is unable to find a mapping between the two.

Error:
Unsupported type RoleType for database postgresql

Fix

The provided fix augments the existing function getJoinIdType to look-up the corresponding field and uses both the type and the kind in the generated join fields.

valtyr commented 6 months ago

Awesome, thanks for your contribution! Please add a changeset (yarn changeset) and I'll merge this :)

arthurfiorette commented 1 month ago

@valtyr any blockers on this?

WKampel commented 3 weeks ago

Can we get this merged?