How can I create a new record of an entity `A` and...
# orm-help
k
How can I create a new record of an entity
A
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?
Copy code
prisma.A.create({
    data: {
       ...,
       B: { connect: { id: ... } }
    }
})
r
@KJReactor 👋 Could you share your schema and Prisma version?
Also another thing to note is that that the behaviour in 2.12.0 changed and that 1-1-relations now must have an optional side which could be related to your issue. As per this schema:
Copy code
model 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:
Copy code
const b = await prisma.b.create({ data: {} })
  await prisma.a.create({ data: { b: { connect: { id: b.id } } } })
k
@Ryan my client is v 2.11
Copy code
model cart {
cart_id        String     @default(cuid()) @id
  user_id        user
  items          cartItem[]
  session        session[]
}
and
Copy code
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])
}
There's of course a user model as well. I'm trying to create a new cartItem but I'm getting an error asking if I meant to use cart_id. I assume the foreign could have been used as well to find the correct cart
r
I think it’s due to the relation defined where
cart
is required in
cartItem
.
Copy code
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:
Copy code
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.
k
Yes, but my issue is that when I do a connect when creating a new cartitem I would like to do using the userid field or cartid rather than only using cart id. So I guess my question is whether foreign keys can be used as a unique key that can be used in
connect
?
to give you a better idea. Here's the query I'm trying to execute:
Copy code
const temp = await prisma.cartItem.create({
        data: {
          qty,
          statusCode: 1,
          product: {
            connect: { product_id }
          },
          cart: {
            connect: {user_id: req.userId}
          }
        },
      })
I get the error:
Copy code
"Unknown arg `user_id` in data.cart.connect.user_id for type cartWhereUniqueInput. Did you mean `cart_id`? Available args:"
r
Ohh, it’s not possible to do a
connect
for a record with a foreign key. In this case, only an id or any unique field can be used.
k
Ok. Well can you suggest any way I might be able to do this without making several querries?
r
If you are unable to use
cartItem_id
then it would require 2 queries, one to fetch the
cartItem_id
via the
userId
and the second to connect.
k
I just wanted to avoid using several queries
Thanks alot @Ryan. I'll just use raw query
💯 1