Hi Guys, the following query returns entries from ...
# orm-help
s
Hi Guys, the following query returns entries from the table 'changeLog' for teamId (specified) and conditional where property 'teamMessageId' & 'commentId' is not null. However, it actually ignores the specified teamId and returns for all teamIds. What am I doing wrong?
Copy code
const teamMsgs = await db.changeLog.findMany({
    where: {
      AND: [
        {
          teamId: teamData?.teamId,
        },
        {
          OR: [
            {
              NOT: {
                teamMessageId: null,
              },
            },
            {
              NOT: {
                commentId: null,
              },
            },
          ],
        },
      ],
    },
    select: {
    ...
r
@SKhan 👋 Could you try this instead?
Copy code
OR: [
  { teamMessageId: { not: null } },
  { commentId: { not: null } },
]
s
Hi Ryan, thank you for looking into this. Unfortunately, that did not work either. I am still getting all the messages in the payload. The updated query:
Copy code
AND: [
        {
          teamId: teamData?.teamId,
        },
        {
          OR: [
            { teamMessageId: { not: null, },},
            {commentId: { not: null, },
            },
           ]
         }
        ], 
        },
        select : {...
Im using Prisma v2.26.0
r
Check the query generated. If the condition is
teamMessageId is not null
then it’s the correct one and the data would not be present that needs to be matched.
s
Thank you Ryan, I have a few db queries in the query. I believe this is the one:
Copy code
SELECT FROM "ChangeLog" WHERE ("ChangeLog"."teamMessageId" IS NOT NULL OR "ChangeLog"."commentId" IS NOT NULL) ORDER BY "ChangeLog"."timeStamp" ASC OFFSET $1 [0]
there is no
teamId
in the query above
I just tried two things: • Rearranged the query as such:
Copy code
await db.changeLog.findMany({
    where: {
      AND: [
        {
          OR: [{ teamMessageId: { not: null } }, { commentId: { not: null } }],
        },
        { teamId: teamData?.teamId,},
      ],
    },
(I pushed the teamId condition below the OR. However, that did not work either. The SQL query is still without teamId condition. • I upgraded to Prisma@3.0.2, however, am still getting the same results
r
Ok. Now I’m pretty sure that’s because
teamData?.teamId
is returning
undefined
. Anything undefined in Prisma means that the condition will not be considered at all.
s
and now I'm pretty sure you are a genius of the highest order. Being clever that I am, I had renamed teamData.id to teamId within a function in the same file and forgot to use teamData.id in this particular instance. This solved it and I feel ... not very clever. HOwever, thank you for saving the day. Again.
💯 1
🙌 1