-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
whereRaw
model query option for Prisma Client queries
#5560
Comments
Additional functionalityWould be awesome if selectRaw could be added as well, to compliment whereRaw.
Finally, raw all the things?
fyi, I am actually using all these from a project that I first developed using laravel and using it's own database builder. But now migrating over to using BlitzJS and Prisma. |
This is definitely needed. TypeORM can do this.
IMO If feature like whereRaw is implemented, there's no need to delete it in the future. |
I want to use PGroonga's special syntax in where clause. pgroonga special syntax is like instead of 'LIKE'
https://pgroonga.github.io/tutorial/ It seems quite difficult to implement this feature in SELECT, and If this feature is implemented, prisma/prisma-client-js#690 issue is also covered by this. full text search is relatively common usage. Would you please consider this feature? |
I also have a use-case, where I want to lowercase the column value It is easy to lowercase the right part, currently there is no way to lowercase the left part before matching the value. |
Needed feature. At this moment you have to resort to raw query or use query builder if your query is a bit more dynamic. I like both TypeORM and MikroORM approaches. Here are some examples, how this could look in Prisma using template literals: prisma.user.findMany({
select: {
[raw`TIME(deletedAt)`]:/* AS */ 'time'
},
where: raw`TIME(deletedAt) > '12:00:00'`,
orderBy: {
[raw`TIME(deletedAt)`]: 'desc'
}
});
prisma.user.findMany({
select: {
[raw`TIME(deletedAt)`]:/* AS */ 'time'
},
where: {
[raw`TIME(deletedAt)`]: {
gt: '12:00:00'
}
},
orderBy: raw`TIME(deletedAt) DESC, TIME(createdAt) ASC`
});
prisma.user.delete({
where: {
[raw`LOWERCASE(name)`]: {
eq: 'my name'
}
}
});
prisma.user.updateMany({
data: {
name: {
set: raw`LOWERCASE(name)`
}
}
});
const email = '';
const name = '';
prisma.user.updateMany({
data: raw`name = ${name}, email = ${email}`
}); |
Having more fine-grained escape hatches like this would be great. It's disappointing to need to resort to a raw query when 95% of your query can be represented in the query builder, but that last 5% means you lose the ergonomics and type safety. |
This would be totally awesome! Ex: I need to make match 1 input to 5 different fields for a "dynamic" search. With a RAW SQL query I can match the id field without converting it to "number" + I can apply date local transformation to adapt the input field, like converting it to "DD-MM-YYY" before comparing.
|
it looks like we may need to wait an another year for this feature: https://www.notion.so/Prisma-Roadmap-50766227b779464ab98899accb98295f |
One would presume that adding a In practice, i consider it a 'must have' for developing small "real world" apps. An other approche could be to have the ability to pass a callback. const mySelect:PrismaQueryBuilder<User> = (qb) => qb.$queryRaw`TIME(deletedAt)`;
const noon = '12:00:00';
prisma.user.findMany({
select: {
$prisma: mySelect,
},
where: {
$prisma: (qb) => qb.$queryRaw`TIME(deletedAt) > ${noon} AND name = firstname`,
},
});
// I could be completely wrong with naming, it's just to picture the idea.
class PrismaQueryBuilder implements Partial<PrismaClient> {} Obviously far more challenging to implement but far more interesting too. Perhaps this suggestion could have more interest for the core team? (Of course with a better written suggestion than this draft) |
I work for a national project. We are starting a study to change of orm. |
I worked around it using views (migrate support for which required a workaround too). So you can embed your function calls into a view and then you can use it with prisma treating it as a table |
This could also help using the postgres jsonb |
We are also in a similar situation, after having already invested much time in writing our data layer in Prisma there are so many unfinished features its very concerning and limiting. |
May I ask what's the status on this one? What's this issue's priority? |
We are busy with other things, when we get to this it will be added to our roadmap at https://pris.ly/roadmap and we will most probably add a comment here asking for more input, share a design or similar. You can "vote" but leaving reaction on features or commenting with your specfic use cases that help us understand the complexity and use case of a feature. |
This is such an essential feature - would love to see it prioritized on the roadmap. There are so many scenarios where the Right now we are forced to go full raw sql statement in these cases, and we ended up needing to write our own "db row -> object" converters for these raw queries - and if we have to do that in the first place, what's the point of needing an ORM? Naively this seems like a relatively simple feature to implement given that mechanism for accept raw sql already exists (haven't looked into Prisma codebase in details, so I could be completely off base) and API interface doesn't need to be complex , and it would go a really long way to make Prisma usable in real world scenarios that don't fit neatly into existing Prisma filtering capabilities. IMO this sits in the sweet "low effort high impact" quadrant and I hope to see it prioritize over some of the larger / more complex projects on the current roadmap. @janpio |
It should support mixing between non-raw and raw. prisma.user.findMany({
where: {
AND: [
{ name: 'Hello' },
raw`updated_at > created_at + INTERVAL '1 hour'`,
]
},
}) |
Oh God!
I am in the same boat. |
If you only need the // Before
prisma.$queryRaw(`... WHERE "fields" @> ${fields || {}}::jsonb)`
// After
const fieldArray = Object.entries(fieldsObject || {})
prisma.widget.findMany({
where: {
AND: fieldArray.length
? fieldArray.map(([key, value]) => ({ fields: { path: [key], equals: value as any } }))
: undefined,
},
orderBy: { id: 'asc' },
take: 5
}) |
@janpio it passed almost a year. Any news on this issue? |
I'm pretty sure the only thing that's lacking is a pull request. Opening issues is one thing, but without pull request it's just a wishlist. |
It's 2024 and we still cannot use raw where like other ORMs |
The people want |
And |
This is exactly it: we all want |
Are there any plans for |
whereRaw
for Prisma Client querieswhereRaw
model query option for Prisma Client queries
I wish to write a query with where condition like: JSON_SEARCH(uids, 'one', '6') IS NOT NULL It seems that it is currently impossible to support frameworks like knex, which will allow the use of raw queries in this small set of conditions, and prisma seems to be able to only fully write sql, while giving up all orm functions |
The way this issue (which is so upvoted/required/discussed/old) is handled by the prisma, shows much disrespect and lack of care towards their community. |
Problem
There is no ability to filter outside of what query options are currently available for where: https://www.prisma.io/docs/concepts/components/prisma-client/filtering-and-sorting
Suggested solution
Have the ability to use whereRaw.
Additional context
whereRaw can be a substitute until actual functionality for specific queries can be introduced into the prisma client.
The text was updated successfully, but these errors were encountered: