Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

orm

prisma

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?

like image 302
Arman Avatar asked Dec 07 '25 04:12

Arman


1 Answers

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

like image 121
Tasin Ishmam Avatar answered Dec 11 '25 20:12

Tasin Ishmam



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!