首页 > 解决方案 > prisma:为什么我不能将“null”设置为可空列的默认值?

问题描述

model Comment {
  id        Int        @id @default(autoincrement())
  reply     Comment?   @relation(fields: [replyId], references: [id], onDelete: SetNull)
  replyId   Int?       @default(null)
  comment   String
}

这里两者replyreplyId都是可以为空的,但是当我尝试迁移时,我得到了这个错误:

error: Error parsing attribute "@default": Expected a numeric value, but received literal value "Null".
  -->  schema.prisma:70
   |
69 |   reply     Comment? @relation(fields: [replyId], references: [id], onDelete: SetNull)
70 |   replyId   Int?     @default(null)
   |

谁能解释这个问题?

标签: ormprisma

解决方案


您实际上不需要为可选字段指定 null 默认值。如果您不指定 Comment关系或明确提供replyId记录的值,则 的值replyId自动为空。对于不充当外键的可选字段也是如此。

因此,您想要的行为会自动出现,无需定义@default(null).


推荐阅读