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

Cannot pass Prisma.empty to $executeRaw function #11233

Closed
mehdi-sp opened this issue Jan 18, 2022 · 5 comments · Fixed by #13656
Closed

Cannot pass Prisma.empty to $executeRaw function #11233

mehdi-sp opened this issue Jan 18, 2022 · 5 comments · Fixed by #13656
Assignees
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: raw $queryRaw(Unsafe) and $executeRaw(Unsafe): https://www.prisma.io/docs/concepts/components/prisma-cli
Milestone

Comments

@mehdi-sp
Copy link

mehdi-sp commented Jan 18, 2022

Bug description

We have a function which generates raw queries based on its input. then the generated query will be passed to $executeRaw to run it. in some conditions, the function may return Prisma.empty. In this case Prisma throws a PrismaClientValidationError

we had no problems in version 2.30.3, This happened after upgrading "prisma" & "@prisma/client" version to 3.8.1

How to reproduce

await db.$executeRaw(Prisma.empty);

throws this error:

    PrismaClientValidationError: `$executeRaw` is a tag function, please use it like the following:
    const result = await prisma.$executeRaw`UPDATE User SET cool = ${true} WHERE email = ${'user@email.com'};`
    
    Or read our docs at https://www.prisma.io/docs/concepts/components/prisma-client/raw-database-access#executeraw

Expected behavior

to run it without any errors. preferably don't issue any queries if the input is Prisma.empty

Prisma information

N/A

Environment & setup

  • OS: Debian
  • Database: PostgreSQL
  • Node.js version: 14.17.6

Prisma Version

10.1.0
@mehdi-sp mehdi-sp added the kind/bug A reported bug. label Jan 18, 2022
@janpio janpio added team/client Issue for team Client. bug/0-unknown Bug is new, does not have information for reproduction or reproduction could not be confirmed. topic: raw $queryRaw(Unsafe) and $executeRaw(Unsafe): https://www.prisma.io/docs/concepts/components/prisma-cli labels Jan 18, 2022
@janpio
Copy link
Member

janpio commented Jan 18, 2022

Did you try to use $executeRaw as tag function as suggested by the output? Between the versions you mentioned the raw queries were overhauled and that was one of the changes. The database page the error links to also spells out the difference between the methods and how they should be used.

@mehdi-sp
Copy link
Author

Did you try to use $executeRaw as tag function as suggested by the output?

Thanks, it works when I use it like this:

await prisma.$executeRaw`${Prisma.empty}`

But still

  1. $executeRaw() accepts arguments with type Prisma.Sql
  2. type of Prisma.empty is Prisma.Sql
    from 1 & 2 I assume that it is safe to have this line in code: await db.$executeRaw(Prisma.empty), but it throws an error

@pantharshit00
Copy link
Contributor

@mehdi-sp executeRaw() (only the function variant overload) was renamed to executeRawUnsafe in the 3.0.1(https://github.com/prisma/prisma/releases/tag/3.0.1, see the $queryRaw API change section) update so please use that now.

@mehdi-sp
Copy link
Author

mehdi-sp commented Jan 19, 2022

As I understand executeRawUnsafe() is not exactly equivalent to executeRaw(). the first one only accepts string query, but return type of my query generator function is Prisma.Sql. it could generate a complex query with lots of Prisma.joins, Prisma.raws & Prisma.sqls or just return a Prisma.empty.

function generateQuery(params): Prisma.Sql { 
   if(someCondition) return Prisma.empty;
   return Prisma.join...;
}

const [item] = await prisma.$transaction([
  prisma.items.create(...),
  prisma.$executeRaw(generateQuery(params))
]);

I can change generateQuerys return type to Prisma.Sql | null and conditionally execute the returned query, but I think it would be nice to allow await db.$executeRaw(Prisma.empty) as it has no type errors (only runtime error)

@pantharshit00 pantharshit00 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 Jun 2, 2022
@danstarns danstarns self-assigned this Jun 6, 2022
@danstarns
Copy link
Contributor

Also applies to $queryRaw.

@janpio janpio added this to the 4.0.0 milestone Jun 24, 2022
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: raw $queryRaw(Unsafe) and $executeRaw(Unsafe): https://www.prisma.io/docs/concepts/components/prisma-cli
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants