Skip to content

Commit

Permalink
⚡️ Update: tag에 constraint 추가
Browse files Browse the repository at this point in the history
같은 유저가 여러 이름의 태그를 가질 수 없도록 제약 조건 추가 및 이에 맞게 seed 파일 수정

#
  • Loading branch information
ks1ksi committed Aug 8, 2023
1 parent d40a6b2 commit 112aea9
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 10 deletions.
8 changes: 8 additions & 0 deletions prisma/migrations/20230808162407_tag_constraint/migration.sql
Original file line number Diff line number Diff line change
@@ -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");
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ model Tag {
createdAt DateTime @default(now()) @map("created_at")
updatedAt DateTime @updatedAt @map("updated_at")
@@unique([name, userId])
@@map("tag")
}

Expand Down
24 changes: 14 additions & 10 deletions prisma/seed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand All @@ -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,
},
},
},
}),
Expand Down

0 comments on commit 112aea9

Please sign in to comment.