Search code examples
ormprisma

Why "null" can't be the default value of a nullable column?


Consider this schema:

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

Here both reply and replyId are nullable. When I migrate I get this error:

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)
   |

Why?


Solution

  • You don't really need to specify a null default value for an optional field. If you don't specify Comment relation or explicitly provide the replyId value for a record, then the value of replyId is automatically null. This is true for optional fields that are not acting as foreign keys as well.

    So, the behavior you want would be there automatically, there's no need to define @default(null).