From 4ea1b51d6e66aa6ee3fddee0eb7a0aa3a4a9e633 Mon Sep 17 00:00:00 2001 From: Alexander Petkov Date: Mon, 1 Jul 2024 15:31:36 +0300 Subject: [PATCH] fix(seed.ts): Fix seeding script failing due to unique constraint --- db/seed/donationWish/seed.ts | 51 ++++++++++++++++++------------------ 1 file changed, 25 insertions(+), 26 deletions(-) diff --git a/db/seed/donationWish/seed.ts b/db/seed/donationWish/seed.ts index 491f99cd6..c06f1fa91 100644 --- a/db/seed/donationWish/seed.ts +++ b/db/seed/donationWish/seed.ts @@ -10,36 +10,35 @@ export async function donationsWishesSeed() { throw new Error('There are no campaigns created yet!') } - const donations = await prisma.donation.findMany() - if (!donations) { + const donation = await prisma.donation.findFirst() + if (!donation) { throw new Error('There are no donations created yet!') } - for (const donation of donations) { - const person = await prisma.person.findFirst() - if (!person) { - throw new Error('There are no people created yet!') - } - const campaign = await prisma.campaign.findFirst({ - where: { - vaults: { - some: { - id: donation.targetVaultId, - }, + const person = await prisma.person.findFirst() + if (!person) { + throw new Error('There are no people created yet!') + } + const campaign = await prisma.campaign.findFirst({ + where: { + vaults: { + some: { + id: donation.targetVaultId, }, }, - }) - if (!campaign) { - throw new Error('There are no campaign with this vault created yet!') - } - const donationWishData = donationWishFactory.build({ - campaignId: campaign.id, - personId: person.id, - donationId: donation.id, - }) - - await prisma.donationWish.create({ - data: donationWishData, - }) + }, + }) + if (!campaign) { + throw new Error('There are no campaign with this vault created yet!') } + const donationWishData = donationWishFactory.build({ + campaignId: campaign.id, + personId: person.id, + donationId: donation.id, + }) + + await prisma.donationWish.createMany({ + data: [donationWishData], + skipDuplicates: true, + }) }