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 error 42601 occurs when using _relevance parameter #24042

Open
kodwi opened this issue Apr 30, 2024 · 3 comments
Open

Postgres error 42601 occurs when using _relevance parameter #24042

kodwi opened this issue Apr 30, 2024 · 3 comments
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client. topic: fullTextSearch topic: postgresql topic _relevance

Comments

@kodwi
Copy link

kodwi commented Apr 30, 2024

Bug description

I'm faced with the error when I try to search text containing two words separated by a space. So if the search text doesn't match any data in db, then it works fine, but if there is some matching data - the error occurs.

The error:

ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(PostgresError { code: "42601", message: "syntax error in tsquery: \"first sec\"", severity: "ERROR", detail: None, column: None, hint: None }), transient: false })

The query that I try to send is in prisma information section. It contains _relevance field, and I think the problem is in its handling (without this field the query works as expected in any cases).

How to reproduce

For example, if I send a string kinda first sec, and the db has a record with first second, so it will crash with the db (postgres) error 42601. But if I send first se1 (two words, but there is no data matching this text) or any string containing one word, it will work.

Expected behavior

No response

Prisma information

this._prismaService.country.findMany({
      where: {
        name: {
          contains: searchText,
          mode: 'insensitive',
        },
      },
      orderBy: {
        _relevance: {
          fields: ['name'],
          search: searchText,
          sort: 'desc',
        },
      },
      take: COMMON.COUNTRY_COUNT,
    });

Environment & setup

  • OS: Windows 10
  • Database: PostgreSQL 15
  • Node.js version: 20.11.0

Prisma Version

prisma                  : 5.13.0
@prisma/client          : 5.13.0
Computed binaryTarget   : windows
Operating System        : win32
Architecture            : x64
Node.js                 : v20.11.0
Query Engine (Node-API) : libquery-engine b9a39a7ee606c28e3455d0fd60e78c3ba82b1a2b (at node_modules\@prisma\engines\query_engine-windows.dll.node)
Schema Engine           : schema-engine-cli b9a39a7ee606c28e3455d0fd60e78c3ba82b1a2b (at node_modules\@prisma\engines\schema-engine-windows.exe)
Schema Wasm             : @prisma/prisma-schema-wasm 5.13.0-23.b9a39a7ee606c28e3455d0fd60e78c3ba82b1a2b
Default Engines Hash    : b9a39a7ee606c28e3455d0fd60e78c3ba82b1a2b
Studio                  : 0.500.0
Preview Features        : fullTextSearch
@kodwi kodwi added the kind/bug A reported bug. label Apr 30, 2024
@kodwi kodwi changed the title Postgres error 42601 occurs when using _relevance parameter Postgres error 42601 occurs when using _relevance parameter Apr 30, 2024
@SevInf SevInf added bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. team/client Issue for team Client. topic: fullTextSearch labels May 2, 2024
@SevInf
Copy link
Contributor

SevInf commented May 2, 2024

Hi @kodwi.
Could you please share your prisma schema? Thank you.

@kodwi
Copy link
Author

kodwi commented May 2, 2024

@SevInf sure

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

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

model Country {
  id     Int    @id @default(autoincrement())
  name   String @unique
  cities City[]
}

model City {
  id        Int        @id @default(autoincrement())
  name      String
  region    String
  countryId Int
  country   Country    @relation(fields: [countryId], references: [id])
  locations Location[]
}

model Location {
  id        String  @id @default(uuid())
  cityId    Int
  city      City    @relation(fields: [cityId], references: [id])
  address   String?
  latitude  Float?
  longitude Float?
}

@SevInf SevInf added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. and removed bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. labels May 3, 2024
@coooola
Copy link

coooola commented May 8, 2024

@kodwi
I faced exactly the same problem than you. If you look for a field with a space and that there is a result for it, I have the same error.
I've came up with this workaround for now: I'm just replacing spaces by comas in the searched terms. You still have the correct results in the right order.

In your example it would do:

this._prismaService.country.findMany({
      where: {
        name: {
          contains: searchText,
          mode: 'insensitive',
        },
      },
      orderBy: {
        _relevance: {
          fields: ['name'],
          search: searchText.split(' ').join(','),
          sort: 'desc',
        },
      },
      take: COMMON.COUNTRY_COUNT,
    });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client. topic: fullTextSearch topic: postgresql topic _relevance
Projects
None yet
Development

No branches or pull requests

4 participants