Hello, all. How can this code ```if (!userId) { ...
# orm-help
n
Hello, all. How can this code
Copy code
if (!userId) {
  return;
}
let newUser;
const checkIfNewUserMigration = await prismaClient.user.findFirst({
  where: {
    systemId: userId,
  },
  select: {
    systemId: true,
    id: true,
  },
});
if (!checkIfNewUserMigration) {
  newUser = await prismaClient.user.create({
    data: {
      systemId: userId,
      firstName: firstName,
      lastName: lastName,
      avatar: avatar,
      conversationMemberships: {
        create: {
          conversation: { connect: { id: conversation.id } },
        },
      },
    },
    select: {
      id: true,
      systemId: true,
    },
  });
} else {
  newUser = checkIfNewUserMigration;
}
produce this error
Copy code
throw new import_engine_core.PrismaClientKnownRequestError(message, e.code, this.prisma._clientVersion, e.meta);
                ^
Error: 
Invalid `prisma.user.create()` invocation:


  Unique constraint failed on the constraint: `systemId_unique`
? There are explicit checks in the code to avoid this as you can see. Can also provide schema etc. if needed? Also do add, on the sample batch data I'm testing this it randomly breaks, so it isn't specific data which causes the issues, sometimes it is the 100. entry, sometimes 130.
s
Hey just real quick, which version of the Prisma are you using?
The reason I'm asking is if this is a unique constraint, then the best thing to do would be to find using
findUnique
n
version
2.21.2
i will try it with
findUnique
still gives the same error, plus this code shouldn't fail in it original format with
findFirst
, it doesnt make sense
s
Can you add some logs? Like so:
Copy code
if (!userId) {
  return;
}
let newUser;
const checkIfNewUserMigration = await prismaClient.user.findFirst({
  where: {
    systemId: userId,
  },
  select: {
    systemId: true,
    id: true,
  },
});
if (!checkIfNewUserMigration) {
  console.log("No checkIfNewUserMigration", checkIfNewUserMigration);
  newUser = await prismaClient.user.create({
    data: {
      systemId: userId,
      firstName: firstName,
      lastName: lastName,
      avatar: avatar,
      conversationMemberships: {
        create: {
          conversation: { connect: { id: conversation.id } },
        },
      },
    },
    select: {
      id: true,
      systemId: true,
    },
  });
} else {
  newUser = checkIfNewUserMigration;
}
And paste what you get here?
n
i already have extensive logging in place, it will not help. to elaborate a little bit, i'm migrating user data from one database to another (which is running with prisma). this goes over rabbitmq & there are numerous jobs back and forth to sync all the migrated data between the two dbs.
s
In this case, is it possible that some other job has synced the data already? Because from the error it sure does seem so
n
just to give an update 🙂 i managed to solve the problem, it was some type of race condition with all the jobs I had running, so not a prisma issue really. thanks for the help
s
Ah, thought so. But Im glad you could figure it out!