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
21.44k stars 484 forks source link

[BUG]: `drizzle-kit generate` doesn't pick up changes in schema 🤔 #2505

Closed holahoon closed 2 weeks ago

holahoon commented 2 weeks ago

What version of drizzle-orm are you using?

0.31.2

What version of drizzle-kit are you using?

0.22.7

Describe the Bug

Running drizzle-kit generate doesn't detect schema changes and doesn't generate updated migration file. I dropped all of my migrations and cleared/deleted all the tables from the database(postgres) so that I can start fresh. Made changes to the table schema and ran drizzle-kit generate --config=drizzle.config.ts to start generating the updated schemas. Some changes in certain tables were picked up, but other tables were missing the newly created columns. I wonder why only partial columns were recognized when generating.

Expected behavior

// specialty.schema.ts

import { relations } from 'drizzle-orm'
import { pgTable, varchar } from 'drizzle-orm/pg-core'
import { generateId } from '@/lib/helpers'
import { registrant } from './registrant.schema'

export const specialty = pgTable('specialty', {
  id: varchar('id')
    .primaryKey()
    .$defaultFn(() => generateId('specialty')),
  name: varchar('name').notNull(),
  kor: varchar('kor'),
})

export const specialtyRelations = relations(specialty, ({ many }) => ({
  registrant: many(registrant),
}))

export type Specialty = typeof specialty.$inferSelect
export type NewSpecialty = typeof specialty.$inferInsert

I'm expecting the id column with it being the primary key to be added. But looking at the snapshot, it still shows the name column being the primary key(which it was set to be primary key previously, but I changed it by adding id column and assigning this column to be the primary key).

// 0000_snapshot.json
...
"public.specialty": {
      "name": "specialty",
      "schema": "",
      "columns": {
        "name": {
          "name": "name",
          "type": "varchar",
          "primaryKey": true,
          "notNull": true
        },
        "kor": {
          "name": "kor",
          "type": "varchar",
          "primaryKey": false,
          "notNull": false
        }
      },
      "indexes": {},
      "foreignKeys": {},
      "compositePrimaryKeys": {},
      "uniqueConstraints": {}
    },
...

I tried adding an extra testField field to this table:

// specialty.schema.ts

import { relations } from 'drizzle-orm'
import { pgTable, varchar } from 'drizzle-orm/pg-core'
import { generateId } from '@/lib/helpers'
import { registrant } from './registrant.schema'

export const specialty = pgTable('specialty', {
  id: varchar('id')
    .primaryKey()
    .$defaultFn(() => generateId('specialty')),
  testField: varchar('test_field'), // <-- new column
  name: varchar('name').notNull(),
  kor: varchar('kor'),
})

export const specialtyRelations = relations(specialty, ({ many }) => ({
  registrant: many(registrant),
}))

export type Specialty = typeof specialty.$inferSelect
export type NewSpecialty = typeof specialty.$inferInsert

This is the latest snapshot.json file creating after running drizzle-kit generate:

// 0002_sad_maverick.sql
...
"public.specialty": {
      "name": "specialty",
      "schema": "",
      "columns": {
        "name": {
          "name": "name",
          "type": "varchar",
          "primaryKey": true,
          "notNull": true
        },
        "kor": {
          "name": "kor",
          "type": "varchar",
          "primaryKey": false,
          "notNull": false
        }
      },
      "indexes": {},
      "foreignKeys": {},
      "compositePrimaryKeys": {},
      "uniqueConstraints": {}
    },
...

It still doesn't pick up the newly created column test_field.

terminal

Reading config file '/<path>/.../drizzle.config.ts'
16 tables
account 2 columns 0 indexes 1 fks
...(skipped for readability)
specialty 2 columns 0 indexes 0 fks
...(skipped for readability)

No schema changes, nothing to migrate 😴

By looking at the above message, drizzle-kit generate doesn't pick up changes in the table schema files.

Environment & setup

Her's my drizzle.config.ts

// drizzle.config.ts

import { defineConfig } from 'drizzle-kit'
import { env } from '@/lib/env'

export default defineConfig({
  schema: './src/lib/db/schema/*',
  dialect: 'postgresql',
  migrations: {
    table: 'migrations',
    schema: 'public',
  },
  dbCredentials: {
    url: env.DATABASE_URL,
  },
  out: './drizzle',
})
kam-st commented 2 weeks ago

have similar problems. Check following bug.

https://github.com/drizzle-team/drizzle-orm/issues/2499

holahoon commented 2 weeks ago

Found the solution. In my directory where it contains multiple schema files, I named them as <some_name>.schema.ts. I also have an index.ts file which exports all the schemas. I've set the schema field my drizzle.config.ts file set to ./src/lib/db/schema/*. I think the problem relates to how all my <name>.schema.ts files along with theindex.ts file is picked up by drizzle-kit. Not sure what the issue is, but I've set the schema path to ./src/lib/db/schema/index.ts and it works 🤷‍♂️