英文:
Drizzle timestamp datenow invalid default value
问题
我是新手使用 Drizzle ORM,我试图创建一个简单的模式,其中有一个名为 created_at 的列,其默认值为帐户创建日期。这是模式:
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)
}))
当我将它推送到 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}`
}
这很奇怪,它说 created_at 的默认值无效,但它仍然被推送到我的数据库。
英文:
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.
答案1
得分: 0
如下所讨论1,当前的解决方法是使用:
default(sqlCURRENT_TIMESTAMP
)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论