prisma-kysely icon indicating copy to clipboard operation
prisma-kysely copied to clipboard

Handle enum primary keys on many-to-many relationships

Open frannyfx opened this issue 9 months ago • 1 comments

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.

frannyfx avatar May 14 '24 09:05 frannyfx

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

valtyr avatar May 21 '24 15:05 valtyr

@valtyr any blockers on this?

arthurfiorette avatar Oct 11 '24 17:10 arthurfiorette

Can we get this merged?

WKampel avatar Oct 27 '24 15:10 WKampel