I am new to Drizzle ORM and I am trying to make a simple schema where there is a created_at column that has default value of date of when did the account got created. This is the schema
export const users = mysqlTable('users', {
id: serial('id').primaryKey(),
email: varchar('email', { length: 100 }).notNull(),
accountType: varchar('accountType', { length: 15 }).references(() => accountTypes.types),
password: varchar('password', { length: 100 }),
emailVerified: boolean('verified').default(false),
verificationToken: varchar('verificationToken', { length: 256 }),
createdAt: timestamp('created_at').notNull().defaultNow(),
}, (users) => ({
userIndex: uniqueIndex('user_idx').on(users.email)
})
)
I am getting this error whenever I push it to planetscale
Error: target: quiz.-.primary: vttablet: rpc error: code = InvalidArgument desc = Invalid default value for 'created_at' (errno 1067) (sqlstate 42000) (CallerID: r6e50nzpgoxvw6iizt6g): Sql: "alter table users add UNIQUE INDEX user_idx (email)", BindVars: {REDACTED}
at PromiseConnection.query (D:\xampp2\htdocs\quiz\node_modules\drizzle-kit\index.cjs:34740:26)
at Command.<anonymous> (D:\xampp2\htdocs\quiz\node_modules\drizzle-kit\index.cjs:52122:33)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
code: 'ER_INVALID_DEFAULT',
errno: 1067,
sql: 'CREATE UNIQUE INDEX `user_idx` ON `users` (`email`);',
sqlState: '42000',
sqlMessage: `target: quiz.-.primary: vttablet: rpc error: code = InvalidArgument desc = Invalid default value for 'created_at' (errno 1067) (sqlstate 42000) (CallerID: r6e50nzpgoxvw6iizt6g): Sql: "alter table users add UNIQUE INDEX user_idx (email)", BindVars: {REDACTED}`
}
It was so weird, It says that the default value for created_at is invalid but it still got pushed to my database.
As discussed here, the current workaround is to use:
default(sql`CURRENT_TIMESTAMP`)