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.64k stars 650 forks source link

[BUG]: PostgresError: relation "public.artist" does not exist #2891

Open apoorv569 opened 2 months ago

apoorv569 commented 2 months ago

What version of drizzle-orm are you using?

0.33.0

What version of drizzle-kit are you using?

0.24.0

Describe the Bug

When try to run the migration I get the following error,

  npm run db:migrate

> app@0.0.1 db:migrate
> tsx src/lib/database/migrate.ts

{
  severity_local: 'NOTICE',
  severity: 'NOTICE',
  code: '42P06',
  message: 'schema "drizzle" already exists, skipping',
  file: 'schemacmds.c',
  line: '131',
  routine: 'CreateSchemaCommand'
}
{
  severity_local: 'NOTICE',
  severity: 'NOTICE',
  code: '42P07',
  message: 'relation "__drizzle_migrations" already exists, skipping',
  file: 'parse_utilcmd.c',
  line: '217',
  routine: 'transformCreateStmt'
}
node:internal/process/promises:288
            triggerUncaughtException(err, true /* fromPromise */);
            ^

PostgresError: relation "public.artist" does not exist
    at ErrorResponse (file:///home/apoorv/repos/app/node_modules/postgres/src/connection.js:788:26)
    at handle (file:///home/apoorv/repos/app/node_modules/postgres/src/connection.js:474:6)
    at Socket.data (file:///home/apoorv/repos/app/node_modules/postgres/src/connection.js:315:9)
    at Socket.emit (node:events:517:28)
    at addChunk (node:internal/streams/readable:368:12)
    at readableAddChunk (node:internal/streams/readable:341:9)
    at Readable.push (node:internal/streams/readable:278:10)
    at TCP.onStreamRead (node:internal/stream_base_commons:190:23) {
  severity_local: 'ERROR',
  severity: 'ERROR',
  code: '42P01',
  where: 'SQL statement "ALTER TABLE "album" ADD CONSTRAINT "album_artist_id_artist_id_fk" FOREIGN KEY ("artist_id") REFERENCES "public"."artist"("id") ON DELETE cascade ON UPDATE no action"\n' +
    'PL/pgSQL function inline_code_block line 2 at SQL statement',
  file: 'namespace.c',
  line: '427',
  routine: 'RangeVarGetRelidExtended'
}

Node.js v18.19.0

Expected behavior

Migration should run fine.

Environment & setup

This is an app I'm making which uses drizzle as the ORM.

arunsetty commented 2 months ago

Exact same bug. Have been trying to debug it since a couple days.

rajeshbolisetty commented 2 months ago

Same Bug

applying migrations...{ severity_local: 'NOTICE', severity: 'NOTICE', code: '42P06', message: 'schema "public" already exists, skipping', file: 'schemacmds.c', line: '135', routine: 'CreateSchemaCommand' } { severity_local: 'NOTICE', severity: 'NOTICE', code: '42P07', message: 'relation "__drizzle_migrations" already exists, skipping', file: 'parse_utilcmd.c', line: '210', routine: 'transformCreateStmt' }

HatemJerbi commented 2 months ago

Same Bug,

The issue is that when Drizzle-Kit generates a FOREIGN_KEY statement, it automatically includes the "public" schema by default. The "public" schema is added to the refrenced tableName.

I suggest adding an option to drizzle-kit to exclude the schema name from migration files.

errorcodezero commented 1 month ago

Is there any temporary fix for now?

L-Mario564 commented 1 month ago

Can anyone provide steps to reproduce this bug? We'll take a look at this.

kioku commented 3 weeks ago

I encountered a similar issue today, where migrate complained about a missing relation because it didn't know which database schema to look in.

applying migrations...PostgresError: relation "....." does not exist

Setting schemaFilter: public in drizzle.config.ts seems to be a valid workaround. Doc reference for schemaFilter.

drizzle-kit@0.26.2
drizzle-orm@0.35.3