P
Prisma2mo ago
vpjm

Enum with custom value

Hi, does anyone know if it's possible to do this in Prisma :
CREATE TYPE "FileType" AS ENUM ('image/jpeg', 'image/png');
CREATE TYPE "FileType" AS ENUM ('image/jpeg', 'image/png');
Because I can't find a way to specify the enum values like this:
enum FileType {
IMAGE_JPEG = "image/jpeg",
IMAGE_PNG = "image/png"
}
enum FileType {
IMAGE_JPEG = "image/jpeg",
IMAGE_PNG = "image/png"
}
1 Reply
Prisma AI Help
Prisma AI Help2mo ago
You chose to compile with the bot. One moment while I piece together your answer… Currently, Prisma does not directly support specifying custom string values for enum fields in the way you've described. The enum values in Prisma are typically represented by their identifier names without the ability to assign custom string values. However, there is a workaround using the @map attribute that can achieve a similar result. Here's how you could represent your enum in Prisma:
enum FileType {
IMAGE_JPEG @map("image/jpeg")
IMAGE_PNG @map("image/png")
}
enum FileType {
IMAGE_JPEG @map("image/jpeg")
IMAGE_PNG @map("image/png")
}
This approach maps the enum values to the desired string representations in the database. However, it's important to note that in your TypeScript code, you would still refer to these enum values as FileType.IMAGE_JPEG and FileType.IMAGE_PNG. There is an open feature request to allow arbitrary enum values in Prisma, which would provide functionality closer to what you're looking for. You can follow the progress of this issue here: Allow arbitrary enum values. Additionally, there's another related issue discussing the use of @map'ed enum values as TypeScript enum values: Use @map'ed enum values as TS enum values. These issues highlight that the Prisma team is aware of this limitation and is considering improvements to enum handling in future versions. ✅ Great, thanks for letting us know that this solved your question!

Did you find this page helpful?