Akshay Kadam (A2K)
11/25/2021, 10:31 AMprisma
or is there a way to call mutation directly? I am doing Oauth so I get the data on the backend only.
If I do send it back to client using res.send
& then do mutation createUser
, then its 2 steps.
If I don't send it to client but do it on the backend, then I don't use the mutation createUser
but directly do it so its 1 step.
What is your recommended approach?Ryan
11/25/2021, 1:26 PMAkshay Kadam (A2K)
11/25/2021, 1:54 PMinterface ICreateUser {
userId: string
username: string
name: string
}
export const createUser = async ({ userId, username, name }: ICreateUser) => {
return await prisma.user.create({
data: {
id: userId,
username,
name,
settings: {
connect: {
id: userId,
},
},
},
})
}
Akshay Kadam (A2K)
11/25/2021, 2:02 PMuser
to have a settings
column. for each user
, it might be different due to the fact that timezone
& userId
aka user
being different.Akshay Kadam (A2K)
11/25/2021, 2:05 PMmodel User {
id String @id @default(cuid())
username String @unique
name String?
email String? @unique
image String?
// relations
settings Settings? @relation(fields: [settingsId], references: [id], map: "User_settingsId_fkey")
teams Team[]
settingsId String
}
model Settings {
id String @id @default(cuid())
timezone String @default("UTC")
// relations
user User @relation(fields: [userId], references: [id])
userId String
}
Akshay Kadam (A2K)
11/25/2021, 2:05 PMError parsing attribute "@relation": The relation fields `user` on Model `Settings` and `settings` on Model `User` both provide the `references` argument in the @relation attribute. You have to provide it only on one of the two fields.
Error parsing attribute "@relation": The relation fields `user` on Model `Settings` and `settings` on Model `User` both provide the `fields` argument in the @relation attribute. You have to provide it only on one of the two fields.
Akshay Kadam (A2K)
11/25/2021, 2:06 PMid
on Settings
but still gives errorRyan
11/25/2021, 2:36 PMid
on Settings
. When you make a relation optional, you also need to make the corresponding foreign key optional, which is why you are getting this error. settings
and settingsId
both need to be optional.Akshay Kadam (A2K)
11/25/2021, 4:25 PMError:
Invalid `prisma.user.create()` invocation:
An operation failed because it depends on one or more records that were required but not found. No 'Settings' record to connect was found was found for a nested connect on one-to-one relation 'SettingsToUser'.
Akshay Kadam (A2K)
11/25/2021, 4:28 PMmodel User {
id String @id @default(cuid())
username String @unique
name String?
email String? @unique
image String?
// relations
teams Team[]
settings Settings?
settingsId String?
}
model Settings {
id String @id @default(cuid())
timezone String @default("UTC")
// relations
user User @relation(fields: [userId], references: [id])
userId String @unique
}
Akshay Kadam (A2K)
11/25/2021, 4:29 PMAkshay Kadam (A2K)
11/25/2021, 5:00 PMAkshay Kadam (A2K)
11/25/2021, 5:00 PMsettings: { create: {} }