question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Raw bulk insert failing

See original GitHub issue

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

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:14 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
pantharshit00commented, Sep 7, 2022

That was me. I wasn’t able to locally reproduce the problem though. https://prisma.slack.com/archives/CA491RJH0/p1662461878343699

1reaction
Noitidartcommented, Sep 30, 2022

I’m having the same issue. I had to move to queryRawUnsafe.

this is my code that fails

prisma.$queryRaw`INSERT INTO "l_intent" ("english") VALUES ${Prisma.join(
          englishes.map(english => `($E$${english}$E$)`)
        )} RETURNING *`

the error is:

error - PrismaClientKnownRequestError: 
Invalid `prisma.$queryRaw()` invocation:


  Raw query failed. Code: `42601`. Message: `db error: ERROR: syntax error at or near "$1"`
    at Object.request (/Users/noitidart/Documents/The-App/web/node_modules/@prisma/client/runtime/index.js:45405:15)
    at async Proxy._request (/Users/noitidart/Documents/The-App/web/node_modules/@prisma/client/runtime/index.js:46301:18) {
  code: 'P2010',
  clientVersion: '3.14.0',
  meta: {
    code: '42601',
    message: 'db error: ERROR: syntax error at or near "$1"'
  },
  page: '/api/v1/l/admin/projects/upload'
}

It’s interesting because if I run this code it works just fine:

prisma.$queryRaw`INSERT INTO "l_intent" ("english") VALUES ($E$don't do it$E$),($E$build with spaces$E$) RETURNING *`

I’m currently using queryRawUnsafe:

prisma.$queryRawUnsafe(
          `INSERT INTO "l_intent" ("english") VALUES ${englishes
            .map(english => `($E$${english}$E$)`)
            .join(', ')} RETURNING *`
        )
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found