prisma1 icon indicating copy to clipboard operation
prisma1 copied to clipboard

[Mongo] Filtering on a relation with "AND" doesn't work properly.

Open AhmadElsagheer opened this issue 6 years ago • 4 comments

I was trying to filter on a relation attribute using AND and I got zero results. I believe it's a bug in mongo connector. I tried it with MySQL on Prisma cloud (prisma 1.32.0-beta) and it worked fine as I expected.

To reproduce

  1. Create the two models.
type Blog {
  id: ID! @id
  name: String!
  labels: [Label!]! @relation(name: "BlogLabels", link: INLINE)
}

type Label {
  id: ID! @id
  name: String! @unique
}
  1. Create two labels
mutation {
  l1: createLabel(data:{
    name: "x"
  }) {
    name
  }
  l2: createLabel(data:{
    name: "y"
  }) {
    name
  }
}

  1. Create a blog
mutation {
  createBlog(data:{
    name: "blog"
    labels: {
      connect: [
        { name: "x" },
        { name: "y" }
      ]
    }
  }) {
    name
    labels {
      name
    }
  }
}
  1. Run this query
query {
  q1: blogs(where: {
    labels_some:{
      name: "x"
    },
    AND:{
      labels_some:{
        name: "y"
      }
    }
  }) {
    name
  }
}

The result will be

{
  "data": {
    "q1": [
      {
        "name": []
      }
    ]
}

Expected behavior

{
  "data": {
    "q1": [
      {
        "name": "blog"
      }
    ]
  }
}

Versions:

  • Connector: [MongoDB]
  • Prisma Server: [1.32.2]
  • prisma CLI: [prisma/1.32.2 (linux-x64) node-v10.15.3]
  • OS: [Ubuntu 18.04]

AhmadElsagheer avatar May 12 '19 19:05 AhmadElsagheer

Closing as a duplicate of https://github.com/prisma/prisma/issues/3943.

I think you are not understanding the API correctly, read the above issue and try this query:

query {
  q1: blogs(
    where: {
      AND: [{ labels_some: { name: "x" } }, { labels_some: { name: "y" } }]
    }
  ) {
    name
  }
}

Also, you said that this worked with MySQL which shouldn't be the case. Can you please a screenshot of the query?

pantharshit00 avatar May 14 '19 12:05 pantharshit00

@pantharshit00 I got your point, but I don't understand what is wrong with my query?

  • When I write a query like
query {
  blogs {
    where: {
      name_contains: "something",
      labels_some: { name: "x" }
    }
 }
}

I am expecting an ANDing of all conditions inside the where clause (name_contains and labels_some). In addition, AND can take either an array or a single element. Accordingly, a query like

query {
  blogs {
    where: {
      name_contains: "something",
      labels_some: { name: "x" },
      AND: [{ labels_some: { name: "y" } }, { labels_some: { name: "z" } }]
    }
  }
}

should return the blogs whose:

  • name contains "something"
  • labels contain a label with name "x"
  • labels contain a label with name "y" and a label with name "z"

So, I think my query should work fine.

Here is a screenshot from SQL connector which produced the output I expected. Screenshot from 2019-05-16 15-48-34

If my understanding for the query is not correct, can you elaborate for me its semantics so that it will return an empty list? And why is it different from SQL connector?

AhmadElsagheer avatar May 16 '19 13:05 AhmadElsagheer

@pantharshit00 your query didn't work as well using mongo connector Screenshot from 2019-05-16 16-00-52

AhmadElsagheer avatar May 16 '19 14:05 AhmadElsagheer

This is indeed a bug @AhmadElsagheer . Thanks for the detailed reproduction. It is specific to our implementation of relations in Mongo and should only occur there. We're working on a fix.

do4gr avatar May 20 '19 13:05 do4gr