prisma: Raw bulk insert failing
Bug description
I’m attempting to do a bulk raw insert. This is to avoid having to loop through every record, as this has performance implications. I need the IDs from the initial bulk insert as I then need to update other tables, and as per what’s discussed here, createMany does not return the inserted rows.
I have spoken to a Prisma Engineer about this, and have attempted to use the code he supplied me with (which, he says, was working for him).
See: https://github.com/prisma/prisma/issues/8131#issuecomment-1237992626
How to reproduce
I am running the following code, in Nest.js:
const rows = [{ status: 's' }, { status: 'x' }];
const values = rows.map((row) => Prisma.sql`(${row.status})`);
await this.prismaService
.$queryRaw`INSERT INTO "test" (status) VALUES ${Prisma.join(
values,
)} RETURNING *`;
When I run the above, I get the following error back:
prisma:query SELECT 1
prisma:query INSERT INTO "test" (status) VALUES $1 RETURNING *
[Nest] 43379 - 06/09/2022, 13:12:24 ERROR [ExceptionsHandler]
Invalid `prisma.$queryRaw()` invocation:
Raw query failed. Code: `42601`. Message: `db error: ERROR: syntax error at or near "$1"`
Error:
Invalid `prisma.$queryRaw()` invocation:
Raw query failed. Code: `42601`. Message: `db error: ERROR: syntax error at or near "$1"`
Expected behavior
No response
Prisma information
model test {
id Int @id @default(autoincrement())
status String? @db.VarChar
}
Environment & setup
- OS: macOS
- Database: Postgres
- Node.js version: 16.13.1
Prisma Version
4.3.1
About this issue
- Original URL
- State: open
- Created 2 years ago
- Reactions: 1
- Comments: 15 (7 by maintainers)
Note: we could update our dependency and expose
bulkhttps://github.com/prisma/prisma/pull/20977It would look like this:
That was me. I wasn’t able to locally reproduce the problem though. https://prisma.slack.com/archives/CA491RJH0/p1662461878343699
I’m having the same issue. I had to move to
queryRawUnsafe.this is my code that fails
the error is:
It’s interesting because if I run this code it works just fine:
I’m currently using
queryRawUnsafe:Who did you talk to? Knowing that would allow us to skip the “reproduce with no knowledge” step. Thanks.