Skip to content

JSON field can not query for "not equals" #7836

Closed

Description

@njfix6

Bug description

I am trying to query a JSON field for not equals similar to how I see in the docs here: https://www.prisma.io/docs/reference/api-reference/prisma-client-reference#return-all-users-where-name-does-not-equal-eleanor

I am using postgres as the backend.

I have tried the following:

const entries = await this.prismaClient.obj.findMany({
      where: {
        values: {
          path: ["test"],
          not: "test",
        },
    },
});

and

    const entries = await this.prismaClient.obj.findMany({
      where: {
        values: {
          not: {
            path: ["Email"],
            equals: "[email protected]",
          },
        },
      },
    });

and also a few other variations but none of them seem to give me back any results. I have previewFeatures = ["orderByRelation", "filterJson"] so this should be good to go. Please let me know if I'm doing something wrong.

How to reproduce

  1. Create a table obj with a json field values.
  2. Create one row with json {"test", "test"}.
  3. Create another row with json {"foo", "bar"}
  4. Try to grab the data with
 const entries = await this.prismaClient.obj.findMany({
      where: {
        values: {
          path: ["test"],
          not: "test",
        },
    },
});
  1. No results will come out

Expected behavior

I would expect the obj with {"foo", "bar"} to appear when running the query above.

Prisma information

Environment & setup

  • OS: Mac OS,
  • Database: PostgreSQL
  • Node.js version: v15.4.0

Prisma Version

2.20.1

Activity

  1. added
    kind/bug
    on Jun 24, 2021
  2. pantharshit00 commented on Jul 29, 2021

    @pantharshit00
    Contributor

    I can reproduce this with 2.29.0-dev.5. We need to define behavior here whether this filter will be limited to the path key or not. If so the behavior is correct, otherwise it is not.

  3. added this to the 2.30.0 milestone on Aug 11, 2021
  4. modified the milestones: 3.1.0, 3.2.0 on Sep 21, 2021
  5. modified the milestones: 3.2.0, 3.3.0 on Oct 6, 2021
  6. dpetrick commented on Oct 11, 2021

    @dpetrick
    Contributor

    This is not a bug, but a side effect of how SQL databases handle the path extraction and nulls.

    This is your test data:

    • {"test", "test"}
    • {"foo", "bar"}

    Your query searches for path: ["test"], not: "test".
    What happens: {"foo", "bar"} doesn't have the path "test" at all, which results in a path extraction result of NULL in the database. You may know that any comparator featuring null on any side (like NULL != "test") is always false, so the row {"foo", "bar"} is not returned, because it doesn't match. It's the same really in other filter operations: If you have one row with NULL and one with "value" in one column, and you check for not: "value" you will not get the NULL row, instead you need to add a compound filter to check for the NULL separately: OR: [ { col: null }, { col: { not: "value" } } ].

    Tl;dr: You need to write:

    const entries = await this.prismaClient.obj.findMany({
        where: {
            OR: [
                {
                    values: {
                        path: ["test"],
                        not: "test",
                    },
                },
                {
                    values: {
                        path: ["test"],
                        equals: Prisma.DbNull, // Or similar, see the API changes in the Prisma 3 release to filter for nulls.
                    },
                }
            ]
        },
    });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug/2-confirmedkind/bugtopic: filterJson

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions