How can I make an optional `unique` input field `p...
# orm-help

Question

How can I make an optional 'unique' input field 'phone' 'NULL' instead of 'undefined' for missing input?

Answer

You can make the field 'phone' nullable by adding a question mark '?' after the field's data type declaration. To make it default to null instead of undefined, set the field's default value as null at the entity level. PostgreSQL guarantees uniqueness even for null fields but not for undefined values. Additionally, it would help to include the query being invoked and the expected output for better assistance.

n
How can I make an optional
unique
input field
phone
NULL
instead of "`undefined`" for missing input. PostgreSQL guarantees uniqueness even for
NULL
fields but not for
undefined
values.
Copy code
model User {
  id                String          @id @default(cuid())
  email             String          @unique
  phone             String?         @unique
  @@map(name: "user")
}
👀 1
h
Hey Nditah 👋 Could you maybe elaborate? There's the
?
if you need to make anything optional, but I see you've already added that. For making something null instead of undefined, just pass in phone number as null when you're creating it.
1
n
Maybe also share the query which you are invoking and your expected output
☝️ 1
1
n
Thank you @Harsh Singh I made the valud null by default at the entity level. It works.
fast parrot 1
253 Views