Next.js Discord

Discord Forum

Need some help setting the model

Answered
Polar bear posted this in #help-forum
Open in Discord
Avatar
Polar bearOP
I have this model
model Post {
  id           String             @id @default(cuid())
  createdAt    DateTime           @default(now())
  updatedAt    DateTime           @updatedAt
  title        String
  title_       Json?              @db.JsonB
  content      String
  content_     Json?              @db.JsonB
  collectionId String
  collection   Collection         @relation(fields: [collectionId], references: [id], onDelete: Cascade)
  metadata     PostMetadata?      @relation(name: "PostMetadata")
  extraData    Json?              @db.JsonB
  extension    PostsExtensionEnum @default(BASE)
  tags         Tag[]              @relation(name: "PostTags")
  order        Int?
  deletedAt    DateTime?

  @@unique([title, content, collectionId])  
}

the content field will be holding html content, which may a large content.
Answered by Dwarf Hotot
For those variables which store large amount of data you can replace the data type like String according to the below following :


* MySQL : Use TEXT or LONGTEXT for large strings.

* PostgressSQL : Use TEXT
View full answer

32 Replies

Avatar
Polar bearOP
when I try to seed the database for mocking, I get this error:

Error occurred during query execution:
ConnectorError(ConnectorError { user_facing_error: None, kind: QueryError(PostgresError { code: "54000", message: "index row size 2744 exceeds btree version 4 maximum 2704 for index \"Post_title_content_collectionId_key\"", severity: "ERROR", detail: Some("Index row references tuple (16,14) in relation \"Post\"."), column: None, hint: Some("Values larger than 1/3 of a buffer page cannot be indexed.\nConsider a function index of an MD5 hash of the value, or use full text indexing.") }), transient: false })
    at Mn.handleRequestError (D:\code\developing\next\gohary-archive\node_modules\.pnpm\@prisma+client@6.0.1_prisma@6.0.1\node_modules\@prisma\client\runtime\library.js:121:7528)      
    at Mn.handleAndLogRequestError (D:\code\developing\next\gohary-archive\node_modules\.pnpm\@prisma+client@6.0.1_prisma@6.0.1\node_modules\@prisma\client\runtime\library.js:121:6663)
    at Mn.request (D:\code\developing\next\gohary-archive\node_modules\.pnpm\@prisma+client@6.0.1_prisma@6.0.1\node_modules\@prisma\client\runtime\library.js:121:6370)
    at async l (D:\code\developing\next\gohary-archive\node_modules\.pnpm\@prisma+client@6.0.1_prisma@6.0.1\node_modules\@prisma\client\runtime\library.js:130:9633)
    at async seedPosts (D:\code\developing\next\gohary-archive\prisma\seed.ts:142:7)
    at async seed (D:\code\developing\next\gohary-archive\prisma\seed.ts:204:3)
    at async <anonymous> (D:\code\developing\next\gohary-archive\prisma\seed.ts:218:5) {
  clientVersion: '6.0.1'
}
Avatar
Chub mackerel
The error occurs because the unique constraint on the title, content, and collectionId fields is generating a database index that exceeds PostgreSQL's maximum row size limit. This happens because the content field, which stores HTML, can hold a lot of data, and PostgreSQL has trouble handling such large values in a B-tree index.
Avatar
Polar bearOP
ok, I know this, but how I solve it.
I need to make sure that posts are unique also.
Avatar
Polar bearOP
I'm wondering if there is a way to make sure that the post is unique based on the collectionId, content, title but without having index on the content?
Avatar
Polar bearOP
how?
Avatar
content String
index String

?
Avatar
Polar bearOP
are you sure?
why would I add index field
?
Avatar
because you want to make collecitonId, content (without index), and title unique. You can do this with the @@unique syntax but it can't detect the inside of the content as to ignore the index. Therefore the solution is = dont store the index inside the content :/
Avatar
Polar bearOP
do you mean like this:

model Post {
  id           String             @id @default(cuid())
  createdAt    DateTime           @default(now())
  updatedAt    DateTime           @updatedAt
  title        String
  title_       Json?              @db.JsonB
  content      String
  content_     Json?              @db.JsonB
  collectionId String
  collection   Collection         @relation(fields: [collectionId], references: [id], onDelete: Cascade)
  metadata     PostMetadata?      @relation(name: "PostMetadata")
  extraData    Json?              @db.JsonB
  extension    PostsExtensionEnum @default(BASE)
  tags         Tag[]              @relation(name: "PostTags")
  order        Int?
  deletedAt    DateTime?

  @@unique([title, collectionId, content])
  @@index([title, collectionId])
}
Avatar
yeah its supposed to be like that
Avatar
lets solve the html content first. Why are you storing html content in the database in the first place?
Avatar
Polar bearOP
I'm getting it from a text-editor component in the dashboard.
The admin can create posts dynamically.
Avatar
why why does it have to be unique?
Avatar
Polar bearOP
because I don't want the admin to accidentally create a post that is already exists.
there is a special cases in my app where the title and collectionId are not enough to make sure that the post is unique
Avatar
I think that is a flawed thinking since an admin can easily add one "space" to avoid the error and could still be mistaken
im saying that you should handle these logic in the application layer not at the model.
Avatar
Polar bearOP
what do you suggest?
I uhh
I would rethink the UX of the application as to not make the Admin create the same post twice
i.e showing the thumbnail of a post at the admin dashboard of the post list
Avatar
or show the first few words of the post
Avatar
Polar bearOP
What do u mean?
Should I show the first few words of a post if the tries to create a post like it?
Avatar
@Polar bear Should I show the first few words of a post if the tries to create a post like it?
Avatar
like in the list of posts, before creating something, show few words from the post for each posts

something like

Post 1
Lorem ipsum dol...

Post 2
Lorem ipsum dol...

Post 3
Lorem ipsum dol...

so admin would know
also I wouldn't blame the fault of writing the same content to the technology. I think its fully the responsibility of the admin who writes it
Avatar
Dwarf Hotot
For those variables which store large amount of data you can replace the data type like String according to the below following :


* MySQL : Use TEXT or LONGTEXT for large strings.

* PostgressSQL : Use TEXT
Answer