How should I create relational records? via connect or via providing IDs? #774
-
I have a schema like this: model Form {
id Int @id @default(autoincrement())
owner_uid String @db.Uuid
project_id Int
prev_id Int? @unique
title String @db.VarChar(255)
form_schema Json @map("schema")
state FormState
user User? @relation(fields: [owner_uid], references: [id], onDelete: SetNull)
project Project? @relation(fields: [project_id], references: [id], onDelete: Cascade)
next Form? @relation("form_history")
prev Form? @relation("form_history", fields: [prev_id], references: [id], onDelete: SetNull)
form_submissions FormSubmission[]
form_requests FormRequest[]
created_at DateTime @default(now())
updated_at DateTime @default(now()) @updatedAt
@@index([owner_uid], name: "form_owner_uid")
@@index([project_id], name: "form_project_id")
@@index([prev_id], name: "form_project_prev_id")
}
and a form repository like this: @staticmethod
def _prepare(data: FormCreate | FormUpdate | FormUpsert) -> FormCreateInput | FormUpdateInput:
base: FormCreateInput | FormUpdateInput = {
"title": str(data.title),
"state": data.state,
"form_schema": Json(data.form_schema),
"owner_uid": str(data.owner_uid), # should I connect records like this on create or update of form record?
"project_id": data.project_id,
# "project": {"connect": {"id": data.project_id}}, or like this?
# "user": {"connect": {"id": str(data.owner_uid)}},
}
if data.prev_id is not None:
base["prev"] = {"connect": {"id": data.prev_id}}
return base
async def create(self, data: FormCreate) -> Form:
async with self.db:
records = await self.db.form.create(data=self._prepare(data))
self._log(records, None, f"Created form record with ID: {records.id}")
return records My question is: Should I create form records by directly providing What is the purpose of the connect API if we can just provide IDs to the
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
In your case it doesn't matter, it's there to provide a consistent API for cases where you have a 1-M relationship so you can easily connect multiple entries at once. The docs should be more clear about this... |
Beta Was this translation helpful? Give feedback.
In your case it doesn't matter, it's there to provide a consistent API for cases where you have a 1-M relationship so you can easily connect multiple entries at once. The docs should be more clear about this...