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),
})
);
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'
}
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'
}
6 Replies
Pradip Chaudhary
Hey @Ani the problem is that you are using serial instead of integer. Serial is an auto-incrementing data type which is mainly useful for pk as id because it increases by 1 every time you add a record in the table. For your use case as per I can understand, you need an integer for the length which can also have a default value of 0.
Ani
AniOP5mo ago
nope, same problem with an integer
rphlmr ⚡
rphlmr ⚡5mo ago
createTable where does it come from? Drizzle Studio? Here a version with pgTable and unitLength as integer: https://drizzle.run/f5hqvbt6li92pq1hi4xpiz0j
Ani
AniOP5mo ago
create table comes from the multi project schemas: https://orm.drizzle.team/docs/goodies#multi-project-schema ___ i've tried ripping it out and just using pgTable, still getting the same error
Drizzle ORM - Goodies
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
Pradip Chaudhary
Have you pushed the schema to db earlier with serial data type? If yes, then it may be the reason that it's not allowing to change it now
Ani
AniOP5mo ago
nope I originally did it with integer, this error is not happening on every integer with a default in my schema
Want results from more Discord servers?
Add your server