#Fix schema error

6 messages · Page 1 of 1 (latest)

severe thicket
#

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])
}
haughty notchBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

severe thicket
#

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
ocean silo
#

Hello @severe thicket 👋

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])
}
#

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

severe thicket
#

thanks, it worked.