Logo

dev-resources.site

for different kinds of informations.

Resolving the `DO $$` Issue in Drizzle ORM with Nile Postgres

Published at
12/5/2024
Categories
postgres
drizzle
prisma
node
Author
jacksonkasi
Categories
4 categories in total
postgres
open
drizzle
open
prisma
open
node
open
Author
11 person written this
jacksonkasi
open
Resolving the `DO $$` Issue in Drizzle ORM with Nile Postgres

When working with Drizzle ORM on a Nile Postgres database, you might encounter an issue where the migration fails due to the use of DO $$ blocks. This is because Nile Postgres does not currently support DO $$ statements. Here's a simple guide to fix this issue and make your migrations work.

Resolving the  raw `DO $$` endraw  Issue in Drizzle ORM with Nile Postgres


What’s the Problem?

Drizzle ORM sometimes generates SQL with DO $$ blocks for operations like adding foreign key constraints. Here’s an example:

DO $$ BEGIN
 ALTER TABLE "account" ADD CONSTRAINT "account_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
Enter fullscreen mode Exit fullscreen mode

When you try to run this SQL on Nile Postgres, it throws an error because DO $$ is not supported.


The Solution

To resolve this issue, you need to remove the DO $$ blocks and keep only the ALTER TABLE statements. This is sufficient to apply the constraints without errors.


Step-by-Step Fix

  1. Locate the SQL File

    After running drizzle-kit generate, find the generated SQL file for your migrations.

  2. Identify DO $$ Blocks

    Look for all instances of DO $$ in the file. These blocks usually wrap around ALTER TABLE statements for adding constraints.

  3. Modify the SQL

    For each block:

    • Remove the DO $$ BEGIN and EXCEPTION ... END $$; lines.
    • Retain only the ALTER TABLE statement.

Before:

   DO $$ BEGIN
    ALTER TABLE "account" ADD CONSTRAINT "account_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;
   EXCEPTION
    WHEN duplicate_object THEN null;
   END $$;
Enter fullscreen mode Exit fullscreen mode

After:

   ALTER TABLE "account" ADD CONSTRAINT "account_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;
Enter fullscreen mode Exit fullscreen mode
  1. Save Changes

    Save the modified SQL file.

  2. Run the Migration

    Run the migration command again:

   deno task migrate
Enter fullscreen mode Exit fullscreen mode

This should apply the changes successfully.

Resolving the  raw `DO $$` endraw  Issue in Drizzle ORM with Nile Postgres

Resolving the  raw `DO $$` endraw  Issue in Drizzle ORM with Nile Postgres


Example Fix

Here’s an example of a full migration SQL file before and after the fix.

Before:

CREATE TABLE IF NOT EXISTS "account" (
    "id" text PRIMARY KEY NOT NULL,
    "userId" text NOT NULL
);
--> statement-breakpoint
DO $$ BEGIN
 ALTER TABLE "account" ADD CONSTRAINT "account_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;
EXCEPTION
 WHEN duplicate_object THEN null;
END $$;
Enter fullscreen mode Exit fullscreen mode

After:

CREATE TABLE IF NOT EXISTS "account" (
    "id" text PRIMARY KEY NOT NULL,
    "userId" text NOT NULL
);
--> statement-breakpoint
ALTER TABLE "account" ADD CONSTRAINT "account_userId_user_id_fk" FOREIGN KEY ("userId") REFERENCES "public"."user"("id") ON DELETE no action ON UPDATE no action;
Enter fullscreen mode Exit fullscreen mode

Why Does This Work?

The DO $$ blocks are used to handle potential errors, such as adding a constraint that already exists. Since Nile Postgres doesn’t support DO $$, we simplify the operation by assuming the migration is applied on a clean database or managing duplicate errors separately.


Conclusion

If you're using Drizzle ORM with Nile Postgres and encounter the DO $$ issue, simply edit your generated SQL file to remove the unsupported blocks. This quick fix ensures your migrations run smoothly.

Looking forward to the Drizzle team's upcoming update that eliminates the need for this workaround! 😊


If you found this helpful, share it with others who are working with Drizzle ORM and Nile Postgres. Happy coding! 🚀

Check out my repository for more insights and examples: GitHub Repository. 👀

prisma Article's
30 articles in total
Favicon
How to Fix the “Record to Delete Does Not Exist” Error in Prisma
Favicon
Building Type-Safe APIs: Integrating NestJS with Prisma and TypeScript
Favicon
Deploying an Existing Express API + Prisma + Supabase Project to Vercel
Favicon
Exploring the Power of Full-Stack Development with Next.js and Prisma
Favicon
How to integrate GitHub CopilotKit with Prisma Integration into your nextJs project Using OpenAI
Favicon
วิธีทำ Auth API ด้วย Express, JWT, MySQL และ Prisma
Favicon
Prisma
Favicon
How we built "Space-Ease" using Next.js
Favicon
Query Objects Instead of Repositories: A Modern Approach to Data Access
Favicon
Common Data Loss Scenarios & Solutions in Prisma Schema Changes
Favicon
How I Solved Common Prisma ORM Errors: Debugging Tips and Best Practices
Favicon
Prisma 101 baby.
Favicon
Prisma
Favicon
QueryBuilder in Action Part 1
Favicon
Prisma ORM: Revolutionizing Database Interactions
Favicon
Prisma & MongoDB: server to be run as a replica set
Favicon
Using GenAI to Tackle Complex Prisma Model Migrations
Favicon
When Embedded AuthN Meets Embedded AuthZ - Building Multi-Tenant Apps With Better-Auth and ZenStack
Favicon
Building Multi-Tenant Apps Using StackAuth's "Teams" and Next.js
Favicon
The Most Awaited Prisma Course Is Here! 😍
Favicon
**Building a Full-Stack Next.js Starter Kit: Authentication, GraphQL, and Testing**
Favicon
Integrate DAYTONA and let the magic begin....
Favicon
Cloudflare D1 and Prisma: Not a Good Combination (For Now)
Favicon
Resolving the `DO $$` Issue in Drizzle ORM with Nile Postgres
Favicon
Nuxt Authorization: How to Implement Team Role-Based Access Control in Nuxt 3
Favicon
Getting Started with Prisma, SQLite, and Express
Favicon
Senior Developer Advocate
Favicon
Building Multi-Tenant Apps Using Clerk's "Organization" and Next.js
Favicon
How to use ORMs (Prisma / Drizzle / Knex.js) in a TypeScript backend built with Encore.ts
Favicon
Pagination and Sorting with Prisma in TypeScript

Featured ones: