drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too šŸ˜…
https://orm.drizzle.team
Apache License 2.0
24.54k stars 643 forks source link

Trouble with Schema Changes Using drizzle-kit push Command in v0.23.0 #2701

Open prezensy opened 3 months ago

prezensy commented 3 months ago

What version of drizzle-orm are you using?

0.32.1

What version of drizzle-kit are you using?

0.23.0

Describe the Bug

I'm encountering issues with the drizzle-kit push command while trying to make schema changes in my Vercel Postgres database. The problem seems related to sequence dependencies that prevent the sequences from being dropped or modified. This issue started after upgrading to drizzle-kit v0.23.0 and drizzle-orm v0.32.1.

I am using a single database to manage multiple projects, and they are differentiated by table filters. This setup complicates handling sequences and dependencies across different project schemas. Notably, when I downgrade to drizzle-kit v0.22.8, the issue is temporarily bypassed. Downgrading drizzle-orm does not produce errors, indicating that the issue may be specific to the drizzle-kit version.

Error Logs:

error: cannot drop sequence ren_businesses_id_seq because other objects depend on it
    at D:\Programming\Apps\london-fusion-food\node_modules\.pnpm\drizzle-kit@0.23.0\node_modules\drizzle-kit\bin.cjs:79476:15
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Object.query (D:\Programming\Apps\london-fusion-food\node_modules\.pnpm\drizzle-kit@0.23.0\node_modules\drizzle-kit\bin.cjs:121418:26)
    at async pgPush (D:\Programming\Apps\london-fusion-food\node_modules\.pnpm\drizzle-kit@0.23.0\node_modules\drizzle-kit\bin.cjs:124361:13)
    at async _Command.<anonymous> (D:\Programming\Apps\london-fusion-food\node_modules\.pnpm\drizzle-kit@0.23.0\node_modules\drizzle-kit\bin.cjs:131618:7) {
  length: 301,
  severity: 'ERROR',
  code: '2BP01',
  detail: 'default value for column id of table ren_businesses depends on sequence ren_businesses_id_seq',
  hint: 'Use DROP ... CASCADE to drop the dependent objects too.',
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'dependency.c',
  line: '1204',
  routine: 'reportDependentObjects'
}

drizzle.config.ts:

import { defineConfig } from "drizzle-kit";

export default defineConfig({
  dialect: "postgresql",
  schema: "./db/schema.ts",
  dbCredentials: {
    url: process.env.POSTGRES_URL as string,
  },
  tablesFilter: ["lff_*"],
});

Expected behavior

The push command should handle sequence dependencies gracefully or provide an option to manage them without causing errors or requiring a downgrade

Environment & setup

ahmedivy commented 3 months ago

Same issue šŸ„²

AndriiSherman commented 3 months ago

Please try the drizzle-kit@seq_fix version. If that fixes your problem, I'll include it in the latest release asap

I would also suggest using the verbose and strict flags to ensure the push is doing what's expected

chris-allen commented 2 months ago

Using drizzle-kit@seq_fix and drizzle-orm@0.33.0, I still have the issue described by @prezensy.

In my example, I have two projects that use different table prefixes (target_ and other_). My drizzle config:

import type { Config } from 'drizzle-kit';

import { env } from './src/env';

export default {
  schema: './src/schema',
  out: './migrations',
  dialect: 'postgresql',
  dbCredentials: {
    url: env.POSTGRES_URL,
  },
  tablesFilter: ['target_*'],
  verbose: true,
  strict: true,
} satisfies Config;

The output of drizzle-kit push:

[āœ“] Pulling schema from database...

 Warning  You are about to execute current statements:

ALTER TABLE "target_contact" ALTER COLUMN "location" SET DATA TYPE geometry(point);
DROP SEQUENCE "public"."other_client_id_seq";
DROP SEQUENCE "public"."other_client_coding_id_seq";
DROP SEQUENCE "public"."other_client_coding_client_id_seq";
DROP SEQUENCE "public"."other_hotel_id_seq";
DROP SEQUENCE "public"."other_profile_id_seq";

āÆ No, abort
  Yes, I want to execute all statements

You can see in the output it's trying to drop all the sequences with the other_ prefix. I've been forced to downgrade to drizzle-kit@0.22.8 in the meantime.

chris-allen commented 1 month ago

Pinning drizzle-kit to 0.22.8 now prevents using the latest version of drizzle-orm.

drizzle-kit: v0.22.8
drizzle-orm: v0.34.1

This version of drizzle-kit is outdated
Please update drizzle-kit package to the latest version šŸ‘

Any update on this one?