Enum mapped values in typedSQL

Hi! 👋 I’m having trouble using enums with mapped values in typedSql. Here’s what I have:
enum Status {
away
appearOffline @@map("appear_offline")
}
enum Status {
away
appearOffline @@map("appear_offline")
}
And in SQL:
SELECT * FROM "User" WHERE "status"::text = ANY($1)
SELECT * FROM "User" WHERE "status"::text = ANY($1)
Prisma sends ['away', 'appearOffline'] instead of ['away', 'appear_offline'], which doesn’t match the DB values. Prisma version: 6.3.1 Is this expected? Or am I missing something? Thanks!
1 Reply
Prisma AI Help
Prisma AI Help•7d ago
You've gone full bot mode! Let me fetch that AI answer for you ASAP. Yes, this is a known issue with Prisma. When using @map directives on enum values, Prisma doesn't use the mapped values in raw SQL queries. Instead, it uses the enum identifiers (like 'appearOffline') rather than the database values ('appear_offline'). This behavior has been reported in several GitHub issues, including Enum values do not map to underlying raw values and Use @map'ed enum values as TS enum values. The problem occurs because Prisma generates TypeScript enums with the original identifiers as values, not the mapped values:
export const Status: {
away: 'away',
appearOffline: 'appearOffline'
};
export const Status: {
away: 'away',
appearOffline: 'appearOffline'
};
Instead of what you might expect:
export const Status: {
away: 'away',
appearOffline: 'appear_offline'
};
export const Status: {
away: 'away',
appearOffline: 'appear_offline'
};
Workaround A workaround mentioned in the issues is to access the internal mapping through Prisma's DMMF (Data Model Meta Format):
function getEnumDBValue(enumName, enumValue) {
const enumDefinition = Prisma.dmmf.datamodel.enums.find(
(enumDef) => enumDef.name === enumName
);

if (!enumDefinition) {
throw new Error(`Enum ${enumName} not found`);
}

const enumValueDefinition = enumDefinition.values.find(
(valDef) => valDef.name === enumValue
);

if (!enumValueDefinition) {
throw new Error(`Value ${enumValue} not found in enum ${enumName}`);
}

return enumValueDefinition.dbName || enumValue;
}
function getEnumDBValue(enumName, enumValue) {
const enumDefinition = Prisma.dmmf.datamodel.enums.find(
(enumDef) => enumDef.name === enumName
);

if (!enumDefinition) {
throw new Error(`Enum ${enumName} not found`);
}

const enumValueDefinition = enumDefinition.values.find(
(valDef) => valDef.name === enumValue
);

if (!enumValueDefinition) {
throw new Error(`Value ${enumValue} not found in enum ${enumName}`);
}

return enumValueDefinition.dbName || enumValue;
}
Then you could use this function to map your enum values before using them in raw SQL:
const statusValues = ['away', 'appearOffline'].map(val =>
getEnumDBValue('Status', val)
);
// Now statusValues would be ['away', 'appear_offline']
const statusValues = ['away', 'appearOffline'].map(val =>
getEnumDBValue('Status', val)
);
// Now statusValues would be ['away', 'appear_offline']
This issue has been open for quite some time, and there are ongoing discussions about how to address it without introducing breaking changes. I hope this helps clarify the situation! ✅ Great, thanks for letting us know that this solved your question!

Did you find this page helpful?