P
Prisma2mo ago
Nykloon

Get an omitted field back for one query

Hello there, so I have globally configured that the password field for the User model will always be omitted. However, on authentication, I want #findFirst to return all of it's fields including the password. I know I can achieve that by just select:'ing all fields and the password, but is there a way to not hardcode that?
So that the following does return the whole User object instead of just the password or null?
const user = await this.prismaService.user.findFirst({
where: { OR: [{ email: emailOrUsername }, { username: emailOrUsername }] },
select: { password: true }
});
const user = await this.prismaService.user.findFirst({
where: { OR: [{ email: emailOrUsername }, { username: emailOrUsername }] },
select: { password: true }
});
Thanks in advance!
1 Reply
Prisma AI Help
Prisma AI Help2mo ago
You selected the bot's instantaneous insights. Let me compile your answer right away! Yes, you can override a globally omitted field for a single query in Prisma. If you have globally omitted the password field for the User model, you can include it in a specific query by setting omit: { password: false } in your query options. This will return all fields, including password, without needing to hardcode all other fields in select. Here’s how you can do it:
const user = await prisma.user.findFirst({
where: { OR: [{ email: emailOrUsername }, { username: emailOrUsername }] },
omit: { password: false }
});
const user = await prisma.user.findFirst({
where: { OR: [{ email: emailOrUsername }, { username: emailOrUsername }] },
omit: { password: false }
});
This approach tells Prisma to include the password field for this query, and all other fields will be returned as usual. You do not need to specify all fields manually in select—using omit: { password: false } is the recommended way to "undo" a global omit for a single query and get the full object back, including the previously omitted field. Reference: Excluding fields ✅ Great, thanks for letting us know that this solved your question!

Did you find this page helpful?