Raw bulk insert failing
See original GitHub issueBug 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
Issue Analytics
- State:
- Created a year ago
- Comments:14 (6 by maintainers)
Top Results From Across the Web
BULK INSERT (Transact-SQL) - SQL Server - Microsoft Learn
Specifies the number of rows in a batch. Each batch is copied to the server as one transaction. If this fails, SQL Server...
Read more >Simple SQL Bulk Insert not working - Stack Overflow
The bulk load failed. The column is too long in the data file for row 1, column 1. Verify that the field terminator...
Read more >Working With Line Numbers and Errors Using Bulk Insert
Error file specifies the erroneous rows of data, in this example rows 8, 11 and 13 and gives us the type of problem...
Read more >INSERT-SELECT Statement occasionally fails silently and ...
We have a batch import process that BULK INSERTs raw data into staging tables, does some data mapping, and then pushes it to...
Read more >File/Folder name causes SQL Bulk Insert command to fail
I've spent a couple days scratching my head trying to figure how to execute a SQL bulk insert command to get csv file...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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
: