I have a model with these 2 properties. When doing...
# orm-help
s
I have a model with these 2 properties. When doing
prisma.create
on this model do I need to provide both
createdBy
and
owner
or either of them?
n
Hey Shivam đź‘‹ You would need to provide the fields which are defined as required in your schema file, if there is any default attribute assigned to the field then you can skip those. You can skip the relation fields as well, in this case
owner
seems to be a relation field so you could skip it.
s
Hey Nurul, thanks for responding. This is my schema file. So is it all right to pass createdBy here? just double confirming as everywhere in doc its mentioned to use
connect
for F Keys.
n
There are two approaches 1. You could directly set your foreignKey, so in this case, you would directly set the value in createdBy field and won’t have the requirement of using
connect
clause. This is considered unsafe because Prisma doesn’t validate if the id that you are passing in the createdBy field actually exists in the User table or not. 2. Using
connect
clause, here instead of setting createdBy directly you could use the owner field to connect this record to the User model, this is considered as safe because is you are passing a user id which doesn’t exist in User model Prisma would throw an error, so you have an extra layer of validation.
đź’Ż 1