-
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
Allow WHERE conditions to compare columns in same table #5048
Comments
Thanks @five07 for this issue. It does indeed make a lot of sense for us to figure out a way to solve this problem. |
One possible solution to represent: SELECT * FROM Shipment WHERE quantityReceived < quantityShipped; import { PrismaClient, shipment } from '@prisma/client'
const prisma = new PrismaClient()
prisma.shipment.findMany({
where: {
quantityReceived: {
lt: shipment.quantityShipped
}
}
}) Could maybe also use a string Exposing |
After discussing further with the team, this comes with different technical considerations than prisma/prisma-client-js#655 |
Is there anything new on this one? |
Would like to have it as well. Right now I'm using DB functions that calculate and set extra columns so I can filter my queries. |
any update on this ? |
Same problem here, any updates? |
The team is currently heads down in implementing support for composite types, which pushed features like this one by quite a bit. |
any updates? |
I would like to point out that this feature should be able to handle use cases such as these (note: the left join query in particular) SELECT
COUNT(g.id) AS 'total_used',
s.`product_name`,
s.`current_period_start` AS 'current_period_start',
s.`current_period_end` AS 'current_period_end'
FROM `subscription` s
JOIN `user` u ON s.`customer_id` = u.`customer_id`
LEFT JOIN `goat` g ON g.`user_id` = u.`id` AND
g.`date_created` BETWEEN s.`current_period_start` AND s.`current_period_end`
WHERE u.`id` = "00000-00000" AND s.`status` = 'ACTIVE' |
Any updates? |
Here's another use case for this: #11646 |
I'm curious if this is gonna be addressed ? I'm likely over-simplifying the issue but this seems like such a fundamental feature of relational database (amount payed < amount due, quantity received < quantity ordered, delivery date < expected delivery date...) that I can't figure out why it's not handled. I understand that it can be dealt with using raw query but then what is the value of Prisma if one has to resort to raw query for basic query. |
I'm also wondering why this is not possible yet. Feels very odd. |
Any update? |
Any updates? Do you have any ETA about it? |
1 similar comment
Any updates? Do you have any ETA about it? |
I also would like upvote this request. This is an important feature to me. |
This comment was marked as abuse.
This comment was marked as abuse.
I see that the developer @cprieto is working on this task. Let's hope he solves this problem 👍 |
@Ontegor yep, we started designing this feature. We'll post an update here once we have something worth sharing! |
Sounds good! |
any updates? |
Hi, Do you have any ETA about it? Thx |
Hey folks 👋 We've started designing this feature and we’re ready to share our proposal. Please let us know what you think! I'd also like to invite you to jump on a quick call with us to share your thoughts. DesignWe aim to support type-safe field references on all filters (e.g. Let’s consider the following snippet from a model Order {
id String @id
paid Int
due Int
user User @relation(fields: [userId], references: [id])
userId String
}
model User {
id String @id
email String @unique
name String
tags String[]
orders Order[]
} You can now express a query to find all the orders where await prisma.order.findMany({
where: {
paid: {
lt: prisma.order.fields.due // paid < due
}
}
}) This proposal adds a special const { id, paid, due, userId } = prisma.order.fields
const { id, email, name, tags } prisma.user.fields Compile-Time Type-SafetyThe Typescript compiler ensures that comparisons are only possible on fields of the same type. For example, the following will trigger an error while type-checking: await prisma.order.findMany({
where: {
id: { equals: prisma.order.fields.due }
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Type error: id is a string, while amountDue is for integers
}
}) Comparisons are also currently restricted to being on the same model: await prisma.order.findMany({
where: {
id: { equals: prisma.user.fields.name }
// ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
// Type error: name is a field on the User model
}
}) Referencing Scalar ListsFor data sources that support scalar lists (e.g. PostgreSQL), you’ll be also able to reference scalar lists with the await prisma.user.findMany({
where: {
// find all users where name is within a list of tags
name: { in: prisma.user.fields.tags }
}
}) We hope you’ll find a better use case for this feature than our example… 😅 Let us know! LimitationsReferencing fields will only be available in non-unique filters for now. For example, the following would not be possible: await prisma.user.findUnique({
where: {
email: prisma.user.fields.name
}
}) |
Good to see there is a type safety check. But what if I want to compare timestamp (int) and datetime (datetime) column in database? Is it possible to do something like: await prisma.order.findMany({
where: {
id: { equals: { field: prisma.order.fields.due, convertTo: prisma.type.Integer } }
}
}) await prisma.order.findMany({
where: {
createdAtDatetime: { lte: { field: prisma.order.fields.createdAtTimestamp, convertTo: prisma.type.Datetime} }
}
}) |
That looks great, but I'm unsure as to how it works with JOINs. Those are the more realistic scenarios of needing such a feature. |
Thanks for your comment @shtse8. Do you have a use case in mind where you'd like to do this? I'd also be curious what the SQL alternative would look like for this if you have something in mind.
Thanks for your comment @danshapir, can you share an example where you'd need to join with column references? |
@matthewmueller Sure. That's for the simple query, just comparison of fields from a join. |
@SevInf How is this closed? The solution doesn't support JOIN queries... The basically main reason for this requirement. |
Hey @danshapir, thanks for the feedback! We've closed this issue because we've implemented the original use case, but the use case around references across tables could be a solid improvement that we'll consider separately. I've taken a note of your feedback in this issue. We'll be reviewing feedback in that issue in case you have any further suggestions! |
Problem
Prisma should allow
WHERE
conditions to compare columns in same tableSuggested solution
Expand
WHERE
filters to include columns of the same type from the same tableSimple example model
Raw SQL:
Currently for example model:
Could alternately be:
Alternatives
Currently only raw queries work
Thanks!
The text was updated successfully, but these errors were encountered: