Drizzle时间戳datenow无效的默认值

huangapple go评论116阅读模式
英文:

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(&#39;users&#39;, {
    id: serial(&#39;id&#39;).primaryKey(),
    email: varchar(&#39;email&#39;, { length: 100 }).notNull(),
    accountType: varchar(&#39;accountType&#39;, { length: 15 }).references(() =&gt; accountTypes.types),
    password: varchar(&#39;password&#39;, { length: 100 }),
    emailVerified: boolean(&#39;verified&#39;).default(false),
    verificationToken: varchar(&#39;verificationToken&#39;, { length: 256 }),
    createdAt: timestamp(&#39;created_at&#39;).notNull().defaultNow(),
}, (users) =&gt; ({
    userIndex: uniqueIndex(&#39;user_idx&#39;).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 &#39;created_at&#39; (errno 1067) (sqlstate 42000) (CallerID: r6e50nzpgoxvw6iizt6g): Sql: &quot;alter table users add UNIQUE INDEX user_idx (email)&quot;, BindVars: {REDACTED}
    at PromiseConnection.query (D:\xampp2\htdocs\quiz\node_modules\drizzle-kit\index.cjs:34740:26)  
    at Command.&lt;anonymous&gt; (D:\xampp2\htdocs\quiz\node_modules\drizzle-kit\index.cjs:52122:33)      
    at process.processTicksAndRejections (node:internal/process/task_queues:95:5) {
  code: &#39;ER_INVALID_DEFAULT&#39;,
  errno: 1067,
  sql: &#39;CREATE UNIQUE INDEX `user_idx` ON `users` (`email`);&#39;,
  sqlState: &#39;42000&#39;,
  sqlMessage: `target: quiz.-.primary: vttablet: rpc error: code = InvalidArgument desc = Invalid default value for &#39;created_at&#39; (errno 1067) (sqlstate 42000) (CallerID: r6e50nzpgoxvw6iizt6g): Sql: &quot;alter table users add UNIQUE INDEX user_idx (email)&quot;, 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.

Drizzle时间戳datenow无效的默认值

答案1

得分: 0

如下所讨论1,当前的解决方法是使用:

default(sqlCURRENT_TIMESTAMP)

英文:

As discussed here, the current workaround is to use:

default(sql`CURRENT_TIMESTAMP`)

huangapple
  • 本文由 发表于 2023年7月13日 19:19:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76678778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定