error: cannot use column reference in DEFAULT expression

in this schema:

export const courses = createTable('courses', {
  id: text('cuid').primaryKey().$defaultFn(() => createId()),
  subjectId: text('subject_id').notNull(),
  name: text('name').notNull(),
  isPublic: boolean('is_public').default(false).notNull(),
  imageUrl: text('image_url').default('/placeholder.png').notNull(),
  unitLength: serial("unit_length").default(0).notNull(),
  description: text('description').notNull()
},
  (t) => ({
    isPublicIdx: index('isPublicCourseIdx').on(t.isPublic),
  })
);


I am trying to add unitLength, but it's throwing this error when I have a default value, I get the same error with an integer.
drizzle-kit: v0.22.8
drizzle-orm: v0.31.2

No config path provided, using default path
Reading config file '/home/blue/Projects/ocw-final/drizzle.config.ts'
Using 'pg' driver for database querying
[✓] Pulling schema from database...
error: cannot use column reference in DEFAULT expression
    at /home/blue/Projects/ocw-final/node_modules/.pnpm/drizzle-kit@0.22.8/node_modules/drizzle-kit/bin.c
js:77696:15
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
    at async Object.query (/home/blue/Projects/ocw-final/node_modules/.pnpm/drizzle-kit@0.22.8/node_modul
es/drizzle-kit/bin.cjs:119635:26)
    at async pgPush (/home/blue/Projects/ocw-final/node_modules/.pnpm/drizzle-kit@0.22.8/node_modules/dri
zzle-kit/bin.cjs:122560:13)
    at async _Command.<anonymous> (/home/blue/Projects/ocw-final/node_modules/.pnpm/drizzle-kit@0.22.8/no
de_modules/drizzle-kit/bin.cjs:129681:7) {
  length: 116,
  severity: 'ERROR',
  code: '0A000',
  detail: undefined,
  hint: undefined,
  position: undefined,
  internalPosition: undefined,
  internalQuery: undefined,
  where: undefined,
  schema: undefined,
  table: undefined,
  column: undefined,
  dataType: undefined,
  constraint: undefined,
  file: 'parse_expr.c',
  line: '563',
  routine: 'transformColumnRef'
}
Was this page helpful?