Hello :wave: I’m setting up Prisma and working on...
# orm-help
a
Hello 👋 I’m setting up Prisma and working on the integration test setup. I have a “reset” step to clear up my DB and using the
TRUNCATE
command (postgres). However, the example here doesn’t work. Throws the following error:
Copy code
PrismaClientValidationError: `$queryRaw` is a tag function, please use it like the following:

const result = await prisma.$queryRaw`SELECT * FROM User WHERE id = ${1} OR email = ${'<mailto:user@email.com|user@email.com>'};`
Now, if I make this change:
Copy code
await prisma.$queryRaw`TRUNCATE TABLE "public"."${tablename}" CASCADE;`;
I get the following error:
Copy code
Invalid `prisma.queryRaw()` invocation:
        
        
Your raw query had an incorrect number of parameters. Expected: `0`, actual: `1`.
I know the other option would be using many
deleteMany()
but I want to try using
TRUNCATE
before I get into the weeds there. I’m using the latest versions (3.0.1)
d
I would generally recommend using Prisma Migrate for such things. Even if you are able to successfully truncate/delete the tables, you will still need to recreate them for the tests to run. You can see how to do this in the following guide: https://www.prisma.io/blog/backend-prisma-typescript-orm-with-postgresql-deployment-bbba1ps7kip5#defining-a-workflow-with-github-actions This is using GitHub Actions to create the database schema and then run the integration tests.
a
So the reset step I’m running before each test, so I have a “clean slate” for each test run. I don’t know where in this guide explains how to do that?
I was using this guide on the prisma documentation as a reference. I have the pipeline to run migrations and set up a docker container as well. I stumbled with the truncate command in this step, where they recommend it as a generic solution https://www.prisma.io/docs/guides/testing/integration-testing#the-test-suite
If anyone stumbles with this, the solution is to use
prisma.$queryRawUnsafe
instead of
prisma.$queryRaw
👍 1
d
m
@Andrés Rodríguez where u able to truncate all tables from db? I ended up doing this
Copy code
for await (const { tablename } of tables) {
    if (tablename !== '_prisma_migrations') {
      try {
        const table: string = tablename;
        await prisma.$executeRawUnsafe(
          ['TRUNCATE TABLE', table, 'CASCADE'].join(' ')
        );
      } catch (error) {
        console.log({ error });
      }
    }
  }
but there is an error when trying to truncate my user table.. it has some data with an index there.. I'm wondering if that is causing the issue
a
From what I can see it is definitely working for me, I’m using
queryRawUnsafe
though not execute
I use the exact example from their docs with that change,
queryRaw
->
queryRawUnsafe