diff --git a/prisma/migrations/20230808162407_tag_constraint/migration.sql b/prisma/migrations/20230808162407_tag_constraint/migration.sql new file mode 100644 index 0000000..b4fa134 --- /dev/null +++ b/prisma/migrations/20230808162407_tag_constraint/migration.sql @@ -0,0 +1,8 @@ +/* + Warnings: + + - A unique constraint covering the columns `[name,user_id]` on the table `tag` will be added. If there are existing duplicate values, this will fail. + +*/ +-- CreateIndex +CREATE UNIQUE INDEX "tag_name_user_id_key" ON "tag"("name", "user_id"); diff --git a/prisma/schema.prisma b/prisma/schema.prisma index 39a8664..def61ec 100644 --- a/prisma/schema.prisma +++ b/prisma/schema.prisma @@ -82,6 +82,7 @@ model Tag { createdAt DateTime @default(now()) @map("created_at") updatedAt DateTime @updatedAt @map("updated_at") + @@unique([name, userId]) @@map("tag") } diff --git a/prisma/seed.ts b/prisma/seed.ts index 82db7e3..80e5bb6 100644 --- a/prisma/seed.ts +++ b/prisma/seed.ts @@ -36,6 +36,17 @@ async function createUserWithDocumentsAndTags(index: number, numDocs: number) { }, }); + const createdTag = await prisma.tag.create({ + data: { + name: `tag${index}`, + user: { + connect: { + id: createdUser.id, + }, + }, + }, + }); + await Promise.all( createdUser.documents.map((doc) => prisma.document.update({ @@ -44,16 +55,9 @@ async function createUserWithDocumentsAndTags(index: number, numDocs: number) { }, data: { tags: { - create: [ - { - name: `tag${index}`, - user: { - connect: { - id: createdUser.id, - }, - }, - }, - ], + connect: { + id: createdTag.id, + }, }, }, }),