Skip to content
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

Postgres "json" column type throws errors with JSON filtering's array_contains #8977

Closed
Tracked by #8628
matthewmueller opened this issue Aug 27, 2021 · 1 comment · Fixed by prisma/prisma-engines#2210
Assignees
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. topic: filterJson topic: previewFeatures Issue touches on an preview feature flag

Comments

@matthewmueller
Copy link
Contributor

matthewmueller commented Aug 27, 2021

Bug description

When trying to select within an array of items, I get the following error

PrismaClientUnknownRequestError3 [PrismaClientUnknownRequestError]: 
Invalid `prisma.users.findMany()` invocation:


  Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(Error { kind: Db, cause: Some(DbError { severity: "ERROR", parsed_severity: Some(Error), code: SqlState("42883"), message: "operator does not exist: json @> unknown", detail: None, hint: Some("No operator matches the given name and argument types. You might need to add explicit type casts."), position: Some(Original(128)), where_: None, schema: None, table: None, column: None, datatype: None, constraint: None, file: Some("parse_oper.c"), line: Some(731), routine: Some("op_error") }) }) })
    at cb (/Users/m/dev/src/github.com/prisma/json-nullability/node_modules/@prisma/client/runtime/index.js:36067:17)
    at processTicksAndRejections (internal/process/task_queues.js:93:5) {
  clientVersion: '2.30.0'
}

How to reproduce

Given the following Postgres Schema

CREATE TABLE users (
    id serial PRIMARY KEY,
    cart json DEFAULT '[]'::json
);

I've seeded this schema with the following:

INSERT INTO "public"."users"("id","cart")
VALUES
(1,E'{"items": ["carrots", "peas"]}'),
(2,E'{"items": [null]}'),
(3,E'{"items": ["apples"]}');

I then ran npx prisma db pull to get the following Prisma Schema:

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["filterJson"]
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model users {
  id   Int   @id @default(autoincrement())
  cart Json? @default("[]") @db.Json
}

Then I ran npx prisma generate and wrote the following script, where I'd like to select the row with the array that contains "carrots":

import { PrismaClient } from "@prisma/client"
const prisma = new PrismaClient()

async function main() {
  const results = await prisma.users.findMany({
    where: {
      cart: {
        path: ["items"],
        array_contains: "carrots",
      },
    },
  })
  console.log(JSON.stringify(results))
}

main()
  .catch(console.error)
  .finally(() => prisma.$disconnect())

Expected behavior

I'd expect to get back the following result:

[{"id":1,"cart":{"items":["carrots","peas"]}}]

You're able to get back this result by changing the column to jsonb:

ALTER TABLE "public"."users" ALTER COLUMN "cart" TYPE jsonb;
ALTER TABLE "public"."users" ALTER COLUMN "cart" SET DEFAULT '[]'::jsonb;

And running the script again

Prisma information

generator client {
  provider        = "prisma-client-js"
  previewFeatures = ["filterJson"]
}

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

model users {
  id   Int   @id @default(autoincrement())
  cart Json? @default("[]") @db.Json
}

Environment & setup

  • OS: OSX
  • Database: Postgres
  • Node.js version: v14.16.0

Prisma Version

Environment variables loaded from .env
prisma                : 2.30.0
@prisma/client        : 2.30.0
Current platform      : darwin
Query Engine (Binary) : query-engine 60b19f4a1de4fe95741da371b4c44a92f4d1adcb (at node_modules/@prisma/engines/query-engine-darwin)
Migration Engine      : migration-engine-cli 60b19f4a1de4fe95741da371b4c44a92f4d1adcb (at node_modules/@prisma/engines/migration-engine-darwin)
Introspection Engine  : introspection-core 60b19f4a1de4fe95741da371b4c44a92f4d1adcb (at node_modules/@prisma/engines/introspection-engine-darwin)
Format Binary         : prisma-fmt 60b19f4a1de4fe95741da371b4c44a92f4d1adcb (at node_modules/@prisma/engines/prisma-fmt-darwin)
Default Engines Hash  : 60b19f4a1de4fe95741da371b4c44a92f4d1adcb
Studio                : 0.422.0
Preview Features      : filterJson
@matthewmueller matthewmueller added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. topic: previewFeatures Issue touches on an preview feature flag topic: filterJson labels Aug 27, 2021
@pantharshit00
Copy link
Contributor

I can reproduce this with 2.31.0-dev.37

@pantharshit00 pantharshit00 added bug/2-confirmed Bug has been reproduced and confirmed. and removed bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. labels Sep 2, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/2-confirmed Bug has been reproduced and confirmed. kind/bug A reported bug. topic: filterJson topic: previewFeatures Issue touches on an preview feature flag
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants