data types errors
Ciao,
i have the following query that highlights an error whenever I try to access "user.userCompany". This query was initially inside a dbClient.$transaction and received the same error. I try to keep outside the transaction, and I'm receiving the same error.
const user = await dbClient.user.findUnique( {
where: {
email: input.email,
},
include: {
userCompany: {
where: {
idCompany: input.idCompany,
status: {
in: [ DBStatus.ACTIVE, DBStatus.PENDING ],
},
},
},
},
} )
If I substitute the "where condition" inside userCompany with "true" everything works (see below)
const user = await dbClient.user.findUnique( {
where: {
email: input.email,
},
include: {
userCompany: true
},
} )
I'm using:
"dependencies": {
"@prisma/client": "^6.11.1",
"@prisma/extension-accelerate": "^2.0.2",
"@prisma/extension-optimize": "^2.0.0",
"@prisma/instrumentation": "^6.11.1",
},
"devDependencies": {
"prisma": "^6.11.1",
}3 Replies
Skipping coffee for instant knowledge! Let's brew that AI answer now.
Your issue is related to using a nested where filter inside the include of a relation in Prisma's findUnique query. According to the knowledge sources, this pattern can cause errors depending on the Prisma version and the structure of your schema.
Key points:
- Using a nested where filter inside include (e.g., userCompany: { where: ... }) is only supported for to-many relations (e.g., hasMany). If userCompany is a to-one relation (hasOne), Prisma does not support filtering with where inside include, and this will result in a type or runtime error. In such cases, you should filter at the top-level where clause instead, or restructure your query logic [Why can't I filter for certain types of joins?].
- If userCompany is a to-many relation, but you are using a Prisma version or preview feature (like relationJoins) that has known issues with nested where filters, you may encounter errors. Several issues have been reported where adding a where clause inside include causes errors, but removing the where clause (i.e., using userCompany: true) works fine [relationJoins: "The table
(not available) does not exist"] [PostgresError: cannot pass more than 100 arguments].
- If you want to filter related records, consider moving the filter to the top-level where clause using the some operator for to-many relations. For example:
This approach is recommended by Prisma maintainers for filtering on nested relations [where condition on nested query many to many relation].
If you are s
till experiencing issues, please check your relation type and Prisma version compatibility. If userCompany is a to-one relation, nested where is not supported. If it is to-many, try the top-level filter as shown above.
🤔 Alright, thanks for letting us know that there's still some help needed. A team member will reply to you shortly.
In the meantime, please let us know any additional info, questions, or which parts of the answer may need clarification.Correct me if I'm wrong, your proposed solution also returns records that have a relation with the user (and don't match filtering criteria) if the query finds at least 1 record that matches filtering criteria.
so i will receive record that have userCompany.idCompany<> input.idCompany
Was this working previously and suddenly you started running into this error?