How does one query for relations that don’t exist,...
# orm-help
m
How does one query for relations that don’t exist, i.e. are
null
? For some reason, the following doesn’t work:
Copy code
await prisma.path.deleteMany({
	where: { AND: [{ Facilitator: { is: null } }, { User: { is: null } }] }
})
r
Hey @martin for a schema like this:
Copy code
model User {
  id    Int    @id @default(autoincrement())
  posts Post[]
}

model Post {
  id     Int   @id @default(autoincrement())
  user   User? @relation(fields: [userId], references: [id])
  userId Int?
}
You can find null relations like this:
Copy code
const users = await prisma.user.findMany({
    where: { posts: { none: { userId: { not: null } } } },
  })
m
Thank you, @Ryan. Unfortunately, that didn’t work either. Here’s the schema:
Copy code
model Path {
  pathId      String       @unique
  Facilitator Facilitator? @relation("OFacilitatorToRPath")
  User        User?        @relation("OUserToRPath")
}

model Facilitator {
  Path              Path               @relation("OFacilitatorToRPath", fields: [pathId], references: [pathId])
  pathId            String             @unique
}

model User {
  Path            Path          @relation("OUserToRPath", fields: [pathId], references: [pathId])
  pathId          String        @unique
}
How can I find `path`s that have neither a
Facilitator
nor a
User
relation?
r
Ahh, it seems you have a 1-1 relationship and not 1-many. I tried your initial query and it works:
Copy code
const paths = await prisma.path.findMany({
    where: {
      AND: [{ Facilitator: { is: null } }, { User: { is: null } }],
    },
})
I added some seed data and this gives me the paths that have neither a
Facilitator
nor a
User
.
m
Thank you, @Ryan, though still getting this error:
Copy code
{} PrismaClientValidationError: 
Invalid `prisma.path.deleteMany()` invocation in test.js:71:22

   67 },
   68 {
   69 	model: 'Path',
   70 	data: async () =>
→  71 		await prisma.path.deleteMany({
        where: {
          AND: [
            {
              Facilitator: {
                is: null
              }
              ~~~~~~~~~~
            },
            {
              User: {
                is: null
              }
            }
          ]
        }
      })

Argument Facilitator: Got invalid value 
{
  is: null
}
on prisma.deleteManyPath. Provided Json, expected FacilitatorRelationFilter or FacilitatorWhereInput or Null.
r
@martin The above query works for me. I’m on version 2.10.0
@prisma/client
and
@prisma/cli
Could you remove
node_modules
, run
prisma generate
and try again?
m
So strange! I have
2.10.2
, deleted
node_modules
and did
prisma generate
and am still getting the same issue.
Maybe this error message can provide us with some clues?
Provided Json, expected FacilitatorRelationFilter or FacilitatorWhereInput or Null.
@Ryan are you sure you tested for
deleteMany
and not
findMany
?
Copy code
await prisma.path.deleteMany({
  where: {
    AND: [{ Facilitator: { is: null } }, { User: { is: null } }]
  }
})
r
@martin yes it was for deleteMany. Let me send you my code.
@martin here’s the code. Let me know if you got it working 🙂
m
Thank you, @Ryan. Haven’t been able to reproduce the error with your test, still not sure why my repo is producing the error. Will get back to you another time! Thanks again.
💯 1