//schema.prisma
model Post {
id String @default(uuid())
name String @db.Text
slug String @unique
tags PostTags[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model PostTags {
id String @default(uuid())
title String
slug String @unique
posts Post[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
//api/route.ts
import prismadb from "@/lib/prismadb";
import { auth } from "@clerk/nextjs/server";
import { NextResponse } from "next/server";
export async function POST(req: Request) {
try {
const { userId } = auth();
const body = await req.json();
const {
name,
slug ,
tags,
} = body;
if (!userId) {
return new NextResponse("Unauthenticated", { status: 401 });
}
if (!name) {
return new NextResponse("Name is required", { status: 400
});
}
if (!slug) {
return new NextResponse("Slug is required", { status: 400
});
if (!tag) {
return new NextResponse("Tag is required", { status: 400 });
}
const post = await prismadb.post.create({
data: {
name,
slug,
tag: {
...
},
},
});
return NextResponse.json(post);
} catch (error) {
console.log("[POST]", error);
return new NextResponse("Internal error", { status: 500 });
}
}
I building a Nextjs app using Prisma for my database, and the app many to many relations between the post and the tags, but I am having a problem with creating and updating a post since I don’t know how to create using m-m relation. I would greatly appreciate your help. Many thanks for considering my request.