Fix schema error

I have this schema:
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model Collection {
id String @id @default(cuid())
title String
parentId String
parent Collection @relation(fields: [parentId], references: [id], name: "SubSeries")
children Collection[] @relation(name: "SubSeries")
description String
posts Post[]
comments Comment[]
tags Tag[] @relation(name: "CollectionTags")

@@map("Series")
}

model Post {
id String @id @default(cuid())
title String
Content String
seriesId String
series Collection @relation(fields: [seriesId], references: [id])
metadata PostMetadata @relation(name: "PostMetadata")
extraData Json @db.JsonB
tags Tag[] @relation(name: "PostTags")
}

model Tag {
id String @id @default(cuid())
name String @unique @db.VarChar(100)
posts Post[] @relation(name: "PostTags")
collections Collection[] @relation(name: "CollectionTags")
}


model PostMetadata {
id String @id @default(cuid())
post Post @relation(name: "PostMetadata",fields: [postId], references: [id])
postId String @unique
views Int @default(0)
isFeatured Boolean @default(false)
publishedAt DateTime?
archivedAt DateTime?
}

model Comment {
id String @id @default(cuid())
content String
seriesId String
series Collection @relation(fields: [seriesId], references: [id])
}
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema

// Looking for ways to speed up your queries, or scale easily with your serverless or edge functions?
// Try Prisma Accelerate: https://pris.ly/cli/accelerate-init

generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

model Collection {
id String @id @default(cuid())
title String
parentId String
parent Collection @relation(fields: [parentId], references: [id], name: "SubSeries")
children Collection[] @relation(name: "SubSeries")
description String
posts Post[]
comments Comment[]
tags Tag[] @relation(name: "CollectionTags")

@@map("Series")
}

model Post {
id String @id @default(cuid())
title String
Content String
seriesId String
series Collection @relation(fields: [seriesId], references: [id])
metadata PostMetadata @relation(name: "PostMetadata")
extraData Json @db.JsonB
tags Tag[] @relation(name: "PostTags")
}

model Tag {
id String @id @default(cuid())
name String @unique @db.VarChar(100)
posts Post[] @relation(name: "PostTags")
collections Collection[] @relation(name: "CollectionTags")
}


model PostMetadata {
id String @id @default(cuid())
post Post @relation(name: "PostMetadata",fields: [postId], references: [id])
postId String @unique
views Int @default(0)
isFeatured Boolean @default(false)
publishedAt DateTime?
archivedAt DateTime?
}

model Comment {
id String @id @default(cuid())
content String
seriesId String
series Collection @relation(fields: [seriesId], references: [id])
}
4 Replies
Anas Badran
Anas BadranOP2w ago
and the error:
Error parsing attribute "@relation": The relation field `post` on Model `PostMetadata` is required. This is not valid because it's not possible to enforce this constraint on the database level. Please change the field type from `Post` to `Post?` to fix this.Prisma
Error parsing attribute "@relation": The relation field `post` on Model `PostMetadata` is required. This is not valid because it's not possible to enforce this constraint on the database level. Please change the field type from `Post` to `Post?` to fix this.Prisma
Nurul
Nurul2w ago
Hello @Anas Badran 👋 Does this updated schema work for you?
model Collection {
id String @id @default(cuid())
title String
parentId String
parent Collection @relation(fields: [parentId], references: [id], name: "SubSeries")
children Collection[] @relation(name: "SubSeries")
description String
posts Post[]
comments Comment[]
tags Tag[] @relation(name: "CollectionTags")

@@map("Series")
}

model Post {
id String @id @default(cuid())
title String
Content String
seriesId String
series Collection @relation(fields: [seriesId], references: [id])
metadata PostMetadata? @relation(name: "PostMetadata")
extraData Json @db.JsonB
tags Tag[] @relation(name: "PostTags")
}

model Tag {
id String @id @default(cuid())
name String @unique @db.VarChar(100)
posts Post[] @relation(name: "PostTags")
collections Collection[] @relation(name: "CollectionTags")
}

model PostMetadata {
id String @id @default(cuid())
post Post? @relation(name: "PostMetadata", fields: [postId], references: [id])
postId String @unique
views Int @default(0)
isFeatured Boolean @default(false)
publishedAt DateTime?
archivedAt DateTime?
}

model Comment {
id String @id @default(cuid())
content String
seriesId String
series Collection @relation(fields: [seriesId], references: [id])
}
model Collection {
id String @id @default(cuid())
title String
parentId String
parent Collection @relation(fields: [parentId], references: [id], name: "SubSeries")
children Collection[] @relation(name: "SubSeries")
description String
posts Post[]
comments Comment[]
tags Tag[] @relation(name: "CollectionTags")

@@map("Series")
}

model Post {
id String @id @default(cuid())
title String
Content String
seriesId String
series Collection @relation(fields: [seriesId], references: [id])
metadata PostMetadata? @relation(name: "PostMetadata")
extraData Json @db.JsonB
tags Tag[] @relation(name: "PostTags")
}

model Tag {
id String @id @default(cuid())
name String @unique @db.VarChar(100)
posts Post[] @relation(name: "PostTags")
collections Collection[] @relation(name: "CollectionTags")
}

model PostMetadata {
id String @id @default(cuid())
post Post? @relation(name: "PostMetadata", fields: [postId], references: [id])
postId String @unique
views Int @default(0)
isFeatured Boolean @default(false)
publishedAt DateTime?
archivedAt DateTime?
}

model Comment {
id String @id @default(cuid())
content String
seriesId String
series Collection @relation(fields: [seriesId], references: [id])
}
Please note that this change doesn't affect the uniqueness constraint on postId, so you'll still have a one-to-one relationship between Post and PostMetadata
Anas Badran
Anas BadranOP2w ago
thanks, it worked.
Nurul
Nurul2w ago
I am glad to hear that! 🙏
Want results from more Discord servers?
Add your server