Skip to content

Sequential $transaction with $executeRaw #4269

@zachasme

Description

@zachasme

Problem

We wish to run a series of $executeRaw queries in sequence using $transaction. Using prisma client 2.11.0, the following:

try {
  await prisma.$transaction([
    prisma.$executeRaw`
      CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
    `,
    prisma.$executeRaw`
      CREATE TABLE "Users" ("id" uuid PRIMARY KEY DEFAULT uuid_generate_v4());
    `,
    prisma.$executeRaw`
      CREATE TABLE "Groups" (userId uuid NOT NULL REFERENCES "Users");
    `,
  ]);
} catch (error) {
  console.error(error);
} finally {
  await prisma.$disconnect();
}

throws

Invalid `prisma.executeRaw()` invocation:


  Raw query failed. Code: `42P01`. Message: `db error: ERROR: relation "Users" does not exist`

Suggested solution

We would like to be able to recreate something like

BEGIN;
CREATE EXTENSION IF NOT EXISTS "uuid-ossp";
CREATE TABLE "Users" ("id" uuid PRIMARY KEY DEFAULT uuid_generate_v4());
CREATE TABLE "Groups" (userId uuid NOT NULL REFERENCES "Users");
COMMIT;

Alternatives

For us, the only alternative is still to keep pg client around for schema migrations.

Additional context

As mentioned in #3828, we have a simple schema migration system and wish replace the current pg client with prisma (which we are using for the rest of the application). The most critical migrations usually require multiple queries so until we can run each migration (which is a set of sql statements) in a transaction we have to keep both PostgreSQL clients.

EDIT: wrong error message, also use extension

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions