Hello! I have some models where fields are being o...
# orm-help
f
Hello! I have some models where fields are being optional when I expect them to be required. Can anyone help shed some light on what I might be doing wrong? 😄 More info in thread 🧵
✅ 1
Here is my model:
Here is the intellisense I get when creating an entry:
So for some reason it thinks that for example questionId is optional, while I consider it a required field Strange thing is that when I import the type model itself, the fields are not marked as optional:
Is it due to the questionId being used for a relation field? Do I need to do anything special to state that the relation field is required?
n
Hey Frederik 👋 I think it depends on the context when it’s required and when not. In this screenshot, it’s not required because the context is to create a new
QuestionAnswer
where you have two modes of linking the new
QuestionAnswer
to a `Question`: • directly set the
questionId
• use a nested write via the
question
relation field Because Prisma Client doesn’t know upfront which of the two you’ll use, it makes both fields optional in the context of a
create
query. Does this help? 🙂
Generally, you should still see a TypeScript error when you provide neither the
questionId
nor the
question
fields, so TypeScript and Prisma Client do prevent you from creating a
QuestionAnswer
without a
Question
.
f
Ah alright! Is there any way I can specify in the schema that I always want to include question id when creating an answer? And that I never want to do a nested write in that scenario?
n
Ha, interesting question! This is currently not possible but it would be interesting for us to consider, feel free to open a new feature request where you describe your problem scenario and ideal solution in a bit more detail 🙂 Also, I’m just curious: How exactly would it improve the experience for you to have field be non-optional in
create
queries? Isn’t the most important thing to get a TS error when you create a new
QuestionAnswer
that doesn’t have
Question
. What exactly is the value in having
questionId
not be optional?
f
Consider the difference in developer experience in these two:
The latter of the two clearly tells me as a developer what I am expected to enter and does not confuse me with props that are irrelevant for me
It is great that I get an error on the data prop itself when I leave out questionId but it is pretty verbose and can be intimidating for a new dev:
n
Nice, yes, that definitely makes a good case for this! Would love to see a feature request with the same reasoning and example 😄 👌
f
Alright, I'll try to write something up 👍
🙏 1
https://github.com/prisma/prisma/issues/14532 Let me know if you think anything is unclear
👌 1
Should have looked through the open issues before going ahead with creating one... 😅 https://github.com/prisma/prisma/issues/14532#issuecomment-1198337046 #TimeWellSpent