model Post { id Int @id @default(autoincrement()) tags Tag[]}model Tag { id Int @id @default(autoincrement()) posts Post[]}
Aspect
Detail
Join table
Auto-named _PostToTag
No extra fields
Can't store metadata on join
2. Defining Explicit Join Table
model PostTag { postId Int tagId Int post Post @relation(fields:[postId], references:[id], onDelete: Cascade) tag Tag @relation(fields:[tagId], references:[id], onDelete: Cascade) @@id([postId, tagId])}
When
Use
Extra fields
e.g., addedAt, addedBy
Custom queries
Need direct join-table access
3. Configuring Join Table Fields
Field
Purpose
Composite PK
@@id([postId, tagId])
Indexes
Add @@index([tagId]) for reverse lookups
Actions
Cascade on both FKs
4. Adding Extra Fields to Join Table
model PostTag { postId Int tagId Int addedAt DateTime @default(now()) addedBy String? @@id([postId, tagId])}