Skip to content

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON filtering not working #23996

Closed
Aloysius999 opened this issue Apr 27, 2024 · 0 comments
Closed

JSON filtering not working #23996

Aloysius999 opened this issue Apr 27, 2024 · 0 comments
Labels
kind/bug A reported bug.

Comments

@Aloysius999
Copy link

Aloysius999 commented Apr 27, 2024

Bug description

I am attempting to query on the content of a JSON field.

My JSON looks like this:

{
	"instrument":{
		"id":90,
		"description":"Mouth organ",
		"hsNumber":"412.132",
		"primaryClassification":"Aerophone",
		"secondaryClassification":"Free aerophone"
	}
}

My prisma query is this:

const items = await prisma.listingItem.findMany({
    where: {
      itemData: {
        path: "$.instrument.description",
        string_contains: "organ",
      },
    },
  });

The query returns 0 records, whereas I'm expecting more than 1 record from the DB.
'

This is an extract from the Prisma-generated SQL query from the log (string quotes removed for formatting to work):

SELECT mydomain.listingItem.id, mydomain.listingItem.userId, mydomain.listingItem.createdAt, mydomain.listingItem.updatedAt, mydomain.listingItem.itemData FROM mydomain.listingItem WHERE (JSON_UNQUOTE(JSON_EXTRACT(mydomain.listingItem.itemData, ?)) LIKE ? AND (JSON_TYPE(JSON_EXTRACT(mydomain.listingItem.itemData, ?)) = ?))

and the log parameters

Params: ["$.instrument.description","%organ%","$.instrument.description","STRING"]

There appears to be no information about the instrument.description path of the JSON_EXTRACT function in the SQL query.

Playing with the query:

SELECT * FROM mydomain.listingItem
WHERE JSON_UNQUOTE(JSON_EXTRACT(mydomain.listingItem.itemData, '$.instrument.description')) LIKE '%organ%'

returns 0 records

How to reproduce

  1. create a DB table listingItem
  2. add a record with this JSON data
  3. query the record using the prisma query above

I'm using Postman to send the query to the API.

Expected behavior

There is at least 1 record in my DB with the word organ contained in the JSON path instrument.description.

No records are being returned.

I expect matching records to be returned.

Prisma information

My prisma schema looks like this:

model listingItem {
  id     String @id @default(cuid())
  userId String

  createdAt DateTime? @default(now())
  updatedAt DateTime? @updatedAt

  itemData Json?
}

Environment & setup

Windows 10 Pro
Version 10.0.19045 Build 19045

MySQL

node -v
v20.10.0

next: "^14.1.4",

Prisma Version

    "prisma": "^5.12.1",

npm prisma -v
10.2.3
@Aloysius999 Aloysius999 added the kind/bug A reported bug. label Apr 27, 2024
@prisma prisma locked and limited conversation to collaborators Apr 29, 2024
@janpio janpio converted this issue into discussion #24008 Apr 29, 2024

This issue was moved to a discussion.

You can continue the conversation there. Go to discussion →

Labels
kind/bug A reported bug.
Projects
None yet
Development

No branches or pull requests

1 participant