Help defining Prisma schema

Hi guys I need some help defining my prisma schema. I have this template schema in which I would like to add tags:
model Template {
id String @id @default(cuid())
name String
description String?
visibility String @default("private")
filename String
content String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
tags TemplateTag[]
}
model Template {
id String @id @default(cuid())
name String
description String?
visibility String @default("private")
filename String
content String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
tags TemplateTag[]
}
So I added the tags field and created a relation to this TemplateTag model:
model TemplateTag {
id String @id @default(cuid())
tag String
Template Template[]
templateId String?
}
model TemplateTag {
id String @id @default(cuid())
tag String
Template Template[]
templateId String?
}
But since the same tag can be used in a different Template, this does not make much sense I think, it would cause redundant data. How do I create a proper pivot table or should I do this differently?
5 Replies
rocawear
rocawear2y ago
https://www.prisma.io/docs/concepts/components/prisma-schema/relations/one-to-many-relations By looking this your TemplateTag should be like
model TemplateTag {
id String @id @default(cuid())
tag String
Template Template @relation(fields: [templateId], references: [id])
templateId String?
}
model TemplateTag {
id String @id @default(cuid())
tag String
Template Template @relation(fields: [templateId], references: [id])
templateId String?
}
utdev
utdevOP2y ago
hmm so what exactly would that change? Saving the tags while saving the template does not seem to work using that relation I am doing this:
const template = await ctx.prisma.template.create({
data: {
user: {
connect: {
id: user.id,
},
},
...input,
tags: {
connect: templateTagIds.map(tagId => ({ id: tagId })),
}
},
});
const template = await ctx.prisma.template.create({
data: {
user: {
connect: {
id: user.id,
},
},
...input,
tags: {
connect: templateTagIds.map(tagId => ({ id: tagId })),
}
},
});
Shouldn't it be this?
model TemplateTag {
id String @id @default(cuid())
tag String @unique
templates TagsOnTemplate[]
}

model Template {
id String @id @default(cuid())
name String
description String?
visibility String @default("private")
filename String
content String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
tags TagsOnTemplate[]
}

model TagsOnTemplate {
id String @id @default(cuid())
template Template @relation(fields: [templateId], references: [id])
templateId String
tag TemplateTag @relation(fields: [tagId], references: [id])
tagId String
}
model TemplateTag {
id String @id @default(cuid())
tag String @unique
templates TagsOnTemplate[]
}

model Template {
id String @id @default(cuid())
name String
description String?
visibility String @default("private")
filename String
content String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
tags TagsOnTemplate[]
}

model TagsOnTemplate {
id String @id @default(cuid())
template Template @relation(fields: [templateId], references: [id])
templateId String
tag TemplateTag @relation(fields: [tagId], references: [id])
tagId String
}
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
utdev
utdevOP2y ago
@FooBarWiggle sooo is my first attempt correct?
Unknown User
Unknown User2y ago
Message Not Public
Sign In & Join Server To View
Want results from more Discord servers?
Add your server