Hello , have a little problem i use `upsert` for C...
# orm-help
b
Hello , have a little problem i use
upsert
for Create if id not exist. So actually update work fine but when id not exist , the create part return me this error :
Copy code
:42:29) {
  code: 'P2023',
  clientVersion: '4.0.0',
  meta: {
    message: 'Error creating UUID, invalid length: expected one of [36, 32], found 35'
  }
}
This is the code
Copy code
const updateFeature = await prisma.feature.upsert({
        where: {
          id: props.idFeature,
        },
        update: {
          cibest_role_id: roleId.id,
          create: props.create,
          read: props.read,
          update: props.update,
          delete: props.delete,
          service_name: props.service_name,
        },
        create: {
          cibest_role_id: roleId.id,
          create: props.create,
          read: props.read,
          update: props.update,
          delete: props.delete,
          service_name: props.service_name,
        },
      });
this is the feature model
Copy code
model feature {
  id             String       @id @default(dbgenerated("gen_random_uuid()")) @db.Uuid
  date_created   DateTime     @default(now()) @db.Timestamp(6)
  service_name   service_name
  create         Boolean      @default(false)
  read           Boolean      @default(false)
  update         Boolean      @default(false)
  delete         Boolean      @default(false)
  cibest_role_id String       @db.Uuid
  iconId         String?      @db.Uuid
  cibest_role    cibest_role  @relation(fields: [cibest_role_id], references: [id])
  icon           icon?        @relation(fields: [iconId], references: [id])
}
1
👀 1
n
Hey Bastien 👋 It seems you are getting an error because the length of the generated UUID is not 32 or 36.
I was able to find this related GitHub Issues: #4060
Can you log the id which is being passed, so the value of
roleId.id
b
yes
1597cc98-5a49-46e8-90da-879d6bf3e857
a uuid form PGSQL
i use upsert cause if id not exist i create new entry with random uuid generate but imposible
n
I just tried the query you shared and when I passed this id - 1597cc98-5a49-46e8-90da-879d6bf3e857, the query worked as expected for me and the record was inserted. Can you try explicitly passing the
id
in
create
clause?
Copy code
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient({
  log: ['query'],
});

async function main() {
  const id = '1597cc98-5a49-46e8-90da-879d6bf3e857';

  const updateFeature = await prisma.feature.upsert({
    where: {
      id: id,
    },
    update: {
      create: false,
      read: false,
      update: false,
      delete: false,
      // service_name: props.service_name,
    },
    create: {
      cibest_role_id: id,
      id: id,
      create: false,
      read: false,
      update: false,
      delete: false,
      // service_name: props.service_name,
    },
  });

  console.log(updateFeature);
}

main()
  .catch((e) => {
    throw e;
  })
  .finally(async () => {
    await prisma.$disconnect();
  });
b
if i pass id in create close probably work , but i need to generate id myself and prisma don't generate himself
Copy code
PrismaClientValidationError:
Invalid `prisma.feature.upsert()` invocation:

{
  where: {
    id: '00000000-0000-0000-0000-000000000000'
  },
  update: {
    cibest_role_id: '7416ca10-d9c5-4e63-a6d5-c295210bbc10',
    create: false,
    read: true,
    update: true,
    delete: false,
    service_name: 'infoVehiculService'
  },
  create: {
    cibest_role_id: '7416ca10-d9c5-4e63-a6d5-c295210bbc10',
    create: false,
    read: true,
    update: true,
    delete: false,
    service_name: 'infoVehiculService',
+   id: String,
?   date_created?: DateTime,
?   iconId?: String | null
  }
}

Argument id for create.id is missing.

Note: Lines with + are required, lines with ? are optional.
like you say in this error
but in my model :
id             _String_       @id @db.Uuid
if i'm not wrong this line generate uuid ?
or i need to generate on node ?
n
In upsert cases you can generate id before hand as well