So I’m trying to create or update a lock. Using up...
# orm-help
k
So I’m trying to create or update a lock. Using upsert. I basically want to create the lock if it does not exist for this particular user OR if it exists just update that one. Currently it changes other peoples locks if the dates (from, to) match.
Copy code
model Lock {
  id     String  @id @default(cuid())
  from   String  @unique
  to     String
  locked Boolean @default(false)
  User   User    @relation(fields: [email], references: [email])
  email  String

  @@unique([from, to, email]) // use can only create one entry with matching from, to and email.
}


model User {
  id            String       @id @default(cuid())
  name          String?
  email         String       @unique
  emailVerified DateTime?
  image         String?
  accounts      Account[]
  sessions      Session[]
  role          Role         @default(USER)
  timereport    Timereport[]
  Lock          Lock[]
}

model Timereport {
  id    String @id @default(cuid())
  date  String
  Event Event  @relation(fields: [event], references: [name])
  event String
  hours Float
  User  User   @relation(fields: [email], references: [email])

  email   String
  Project Project @relation(fields: [project], references: [name])
  project String

  @@unique([date, event, email]) // Do not allow multiple entries of same date + event + email
}
Further my query looks something like this
Copy code
async resolve({ ctx, input }) {
      const email = ctx.session?.user?.email;
      const { from, to, locked } = input;
      const create = await ctx.prisma.lock.upsert({
        where: {
          from: from,
        },
        update: {
          locked: locked,
          from: from,
          to: to,
        },
        create: {
          from,
          to,
          locked,
          email: email!,
        },
      });
      return create;
    },
  })
r
hi @kami gerami - you need to include something to identify the user in the
where
piece
eg:
Copy code
async resolve({ ctx, input }) {
      const email = ctx.session?.user?.email;
      const { from, to, locked } = input;
      const create = await ctx.prisma.lock.upsert({
        where: {
          from: from,
          user: { email: email },
        },
        update: {
          locked: locked,
          from: from,
          to: to,
        },
        create: {
          from,
          to,
          locked,
          email: email!,
        },
      });
      return create;
    },
  })
(I think that syntax should work) it's adding the
user: { email: email },
to the
where
or if that
email
column in your
Lock
table is the user email then you can simplify it to:
Copy code
async resolve({ ctx, input }) {
      const email = ctx.session?.user?.email;
      const { from, to, locked } = input;
      const create = await ctx.prisma.lock.upsert({
        where: {
          from: from,
          email: email,
        },
        update: {
          locked: locked,
          from: from,
          to: to,
        },
        create: {
          from,
          to,
          locked,
          email: email!,
        },
      });
      return create;
    },
  })
k
thnx - yeah I actually did try a few variants but it seems like upsert requires the columns to be unique in the table when we specify the where clause.
getting this now in my ide
saying it also needs to be unique
what if I specify the from, to and email as unique in my prisma schema hmm
no that would not work either because then only one single entry would be possible
r
and f you try
upsertMany
? (if that exists)
k
Don’t believe that actually exists
I know there is an open feature request for it however