await prisma.arbitrated.upsert({ where: { ...
# orm-help
h
await prisma.arbitrated.upsert({ where: { match: { session_id: matchId } }, update: {}, create: { match: { connect: { session_id: matchId } }, value: 1233245354325 } });
r
@Halvor đź‘‹ Could you share your schema?
h
Copy code
model match {
 id                Int                 @id @default(autoincrement())
  createdAt         DateTime            @default(now())

  session_id        BigInt              @unique
  session_key       String
  arbitrated        arbitrated?
}

model arbitrated {
  id                Int                 @id @default(autoincrement())
  createdAt         DateTime            @default(now())
  match             match?              @relation(fields: [matchId], references: [id])
  matchId           Int?

  session_id        BigInt              @unique
}
It should be possible to have a 1-to-1 relation here right?
error TS2339: Property 'arbitrated' does not exist on type 'PrismaClient<PrismaClientOptions, never, RejectOnNotFound | RejectPerOperation | undefined>'.
r
It should be like this:
Copy code
await prisma.arbitrated.upsert({
    where: {
      session_id: matchId,
    },
    update: {},
    create: {
      match: { connect: { session_id: matchId } },
      session_id: matchId,
    },
  })
h
but this would check if session_id in arbitrated table matches "matchId" right?
Maybe confusing since session_id exists in both tables representing different values.
Copy code
const arbitrated = await prisma.arbitrated.upsert({
        where: {
                session_id: matchId
        },
        update: {},
        create: {
            match: {
                connect: {
                    session_id: matchId
                }
            }
        }
    });
@Ryan In the where clause you're proposing you are checking against the attribyte "session_id" of arbitrated right? What i want to do is to check against the 1-to-1 relation of the match table.
@Ryan Is that even possible to do with prisma? See the other conversation 🙂
r
In the where clause you’re proposing you are checking against the attribyte “session_id” of arbitrated right? What i want to do is to check against the 1-to-1 relation of the match table.
This isn’t possible. You can only check on the fields of the model you’re performing an
upsert
on.
It would be better if you could explain your use case.
h
I have a table match, later on optionally for some entries of match i want to add an entry in the arbitrated table.
But i need to check if a arbitrated entry exists for the given entry in the match table.
IN that case i'll return that instead of inserting another one.
Is correct approach to instead update() the match entry and create the relation from there?
r
In this case, why do you need
session_id
in both tables? As it’s a 1-1 relation, you can easily check and return the entry using
upsert
.
h
forget about session_id in arbitrated table, i need to check it against the match table.
but you said that is not possible?
i need to create the arbitrated entry for an already existing entry of match that has session_id equal to a given one.
so maybe upsert is not correct here?
if trying to do this with update() on the match table i get: "The change you are trying to make would violate the required relation 'arbitratedTomatch' between the
arbitrated
and
match
models."