Hey all! I am having some difficulty with seeding ...
# orm-help
s
Hey all! I am having some difficulty with seeding my database (Forgive me I am a little bit new). I am trying to create a modal Vote, but I keep getting an error `Unique constraint failed on the fields: (
userId
)` • the thing that is confusing to me is that I have other modals that are setup very similarly and this is just causing me a lot of confusion. This is my code:
Copy code
const fakers = await Promise.all(
    new Array(10).fill(1).map(async () => {
      return {
        email: faker.internet.email(),
        name: faker.name.fullName(),
        id: faker.datatype.string(),
        username: faker.internet.userName(),
        image: faker.internet.avatar(),
      }
    })
  )

  const fakeUsers = await Promise.all(
    fakers.map(async (user) => {
      return prisma.user.upsert({
        where: { email: user.email },
        update: {},
        create: {
          email: user.email,
          name: user.name,
          image: user.image,
          username: user.username,
        },
      })
    })
  )

  const suggestions = await Promise.all(
    fakeSuggestions.map(async (suggestion, i) => {
      return prisma.suggestion.upsert({
        where: { title: suggestion.title },
        update: {},
        create: {
          title: suggestion.title,
          description: suggestion.description,
          user: {
            connect: {
              id: fakeUsers[Math.floor(Math.random() * fakeUsers.length)].id,
            },
          },
          category: {
            connect: {
              id: cats[Math.floor(Math.random() * cats.length)].id,
            },
          },
          status: {
            connect: {
              type: stats[Math.floor(Math.random() * stats.length)].type,
            },
          },
        },
      })
    })
  )

  // Votes
  await Promise.all(
    new Array(100).fill(1).map(async (i) => {
      return prisma.vote.upsert({
        where: {
          id: Math.floor(Math.random() * 1000),
        },
        update: {},
        create: {
          user: {
            connect: {
              id: fakeUsers[Math.floor(Math.random() * fakeUsers.length)].id,
            },
          },
          suggestion: {
            connect: {
              id: suggestions[Math.floor(Math.random() * suggestions.length)]
                .id,
            },
          },
        },
      })
    })
  )
āœ… 1
This is my Vote modal:
Copy code
model Vote {
  id           Int        @id @default(autoincrement())
  createdAt    DateTime   @default(now())
  updatedAt    DateTime   @updatedAt
  userId       String
  suggestionId Int
  suggestion   Suggestion @relation(fields: [suggestionId], references: [id])
  user         User       @relation(fields: [userId], references: [id])
}
n
Hi Sean šŸ‘‹ Welcome to our community! prisma cool When are you exactly getting this error? Is it during creating votes? Can you enable logging and check where exactly the query fails? Have you defined unique index in any of the models in your schema file?
v
šŸ‘‹ @sean oreilly would you mind also telling us a bit more about this use case? (e.g. personal or work project, in prod, stack, etc.) This will allow us to prioritize and tailor our answers better! šŸ™
s
Sure - @Vladi Stevanovic - this is just a personal project. I actually just made a migration to remove the vote modal and then re-created it, and it seems to have worked. I don't really know what the issue was still.
šŸ‘ 2
v
Thank you for the update Sean and the additional information! I'm glad it's working now but if you come across this issue again please let us know so we can investigate further!