Prisma automatically creates related rows when the parent is created
P粉211600174
2023-08-26 19:19:48
<p>I have a pattern similar to this</p>
<pre class="brush:php;toolbar:false;">model User {
id Int @default(autoincrement())
settingsSettings
}
model Settings {
userId Int
settingOne Boolean @default(false)
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
}</pre>
<p>I don't want the user's settings to be optional - is there a way to automatically create the corresponding rows in the settings table when the user is created? </p>
I'm doing something very similar in my code:
All of our
publications
have a correspondingpublicationStatus
, similar to the question you listed, maybe you can do this:or similar operation?
This is impossible, because if both sides of the relationship are required, how can you create either one? So relational aspects without relational scalars (fields that represent foreign keys in the database) must be optional. You can decide which one yourself.
For example, you want to create
User
, butSettings
is required, so you need to createSettings
first. But to createSettings
you also needUser
because it is required in theSettings
model.For more information please refer to the documentation