KJReactor
11/30/2020, 9:42 PMA
and connect to an existing record of entity B
using B.id
in the connect
field ? the following is not working for me. The relationship is 1:1 so B.id
should be valid as a unique key for connect
, no?
prisma.A.create({
data: {
...,
B: { connect: { id: ... } }
}
})
Ryan
12/01/2020, 6:48 AMRyan
12/01/2020, 8:13 AMmodel A {
id Int @id
b B
bId Int
}
model B {
id Int @id
a A?
}
The code below to connect to the model B
works fine:
const b = await prisma.b.create({ data: {} })
await prisma.a.create({ data: { b: { connect: { id: b.id } } } })
KJReactor
12/01/2020, 12:16 PMKJReactor
12/01/2020, 12:18 PMmodel cart {
cart_id String @default(cuid()) @id
user_id user
items cartItem[]
session session[]
}
and
model cartItem{
cartItem_id Int @default(autoincrement()) @id
cart_id String
createdAt DateTime? @default(now())
statusCode Int
product_id String
qty Int
cart cart @relation(fields: [cart_id], references: [cart_id])
product product @relation(fields: [product_id], references: [product_id])
@@unique([cartItem_id, cart_id, createdAt])
}
KJReactor
12/01/2020, 12:22 PMRyan
12/01/2020, 1:22 PMcart
is required in cartItem
.
const { cartItem_id } = await prisma.cartItem.create({
data: { qty: 1, statusCode: 1 },
})
await prisma.cart.create({
data: { items: { connect: { cartItem_id } } },
})
The above works when I run it with the following schema:
model cart {
cart_id String @id @default(cuid())
items cartItem[]
}
model cartItem {
cartItem_id Int @id @default(autoincrement())
cart_id String?
createdAt DateTime? @default(now())
statusCode Int
qty Int
cart cart? @relation(fields: [cart_id], references: [cart_id])
@@unique([cartItem_id, cart_id, createdAt])
}
Here is where I keep the cart
optional.KJReactor
12/01/2020, 2:11 PMconnect
?KJReactor
12/01/2020, 2:14 PMconst temp = await prisma.cartItem.create({
data: {
qty,
statusCode: 1,
product: {
connect: { product_id }
},
cart: {
connect: {user_id: req.userId}
}
},
})
I get the error:
"Unknown arg `user_id` in data.cart.connect.user_id for type cartWhereUniqueInput. Did you mean `cart_id`? Available args:"
Ryan
12/01/2020, 2:16 PMconnect
for a record with a foreign key. In this case, only an id or any unique field can be used.KJReactor
12/01/2020, 2:25 PMRyan
12/01/2020, 3:52 PMcartItem_id
then it would require 2 queries, one to fetch the cartItem_id
via the userId
and the second to connect.KJReactor
12/01/2020, 3:57 PMKJReactor
12/01/2020, 3:58 PM