-
Notifications
You must be signed in to change notification settings - Fork 0
Add Subpost model and one-to-many relation to Post in Prisma schema #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
Caution Review failedThe pull request is closed. WalkthroughAdds a new Subpost model and Post→Subpost relation in the Prisma schema, restructures Post fields (drops content/categories and adds summary/total fields), removes two GIN indexes, and includes two SQL migrations: one creating Changes
Sequence Diagram(s)sequenceDiagram
participant Client
participant API
participant Prisma
participant DB
rect rgba(200,230,255,0.18)
note right of Client: Create Subpost (new flow)
Client->>API: POST /posts/:postId/subposts { content, sentiment, ... }
API->>Prisma: prisma.subpost.create({ data: { content, sentiment, postId, ... } })
Prisma->>DB: INSERT INTO public.Subpost (...)
DB-->>Prisma: 201 Created (FK validated)
Prisma-->>API: Subpost record
API-->>Client: 201 Created (Subpost)
end
rect rgba(220,255,220,0.14)
note right of Client: Fetch Post with summaries & subposts
Client->>API: GET /posts/:postId?include=subposts
API->>Prisma: prisma.post.findUnique({ include: { subposts: true }})
Prisma->>DB: SELECT Post LEFT JOIN Subpost
DB-->>Prisma: Post (with summary fields) + Subpost[]
Prisma-->>API: Aggregated result
API-->>Client: 200 OK
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
📜 Recent review detailsConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro 📒 Files selected for processing (1)
Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (3)
prisma/schema.prisma (1)
67-74
: IndexpostId
and encode referential actions explicitly in schema.
- Add an index on
postId
(and optionally(postId, createdAt DESC)
) to speed up common lookups.- The migration sets
ON DELETE RESTRICT ON UPDATE CASCADE
, but the schema omits referential actions. Encode them explicitly to prevent drift.Apply:
model Subpost { id String @id @default(uuid()) content String postId String - post Post @relation(fields: [postId], references: [id]) + post Post @relation(fields: [postId], references: [id], onDelete: Restrict, onUpdate: Cascade) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt + + @@index([postId]) + // If you frequently fetch latest-first: + // @@index([postId, createdAt(sort: Desc)]) }Additional notes:
- If you intend parent deletion to remove children, switch
onDelete
toCascade
(and update the migration accordingly). Otherwise keepRestrict
.- If your app relies on absolute UTC semantics, consider
DateTime @db.Timestamptz
forcreatedAt/updatedAt
in both Post and Subpost in a follow-up migration.prisma/migrations/20250915081725_add_subpost_model/migration.sql (2)
1-10
: Add index on Subpost.postId (and optionally on postId,createdAt).PostgreSQL doesn’t auto-index FKs; without it, fetching subposts by post can degrade under load.
If this migration hasn’t been applied anywhere yet, append:
CREATE TABLE "public"."Subpost" ( "id" TEXT NOT NULL, "content" TEXT NOT NULL, "postId" TEXT NOT NULL, "createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP, "updatedAt" TIMESTAMP(3) NOT NULL, CONSTRAINT "Subpost_pkey" PRIMARY KEY ("id") ); + +-- Speed up lookups by parent and recency +CREATE INDEX "Subpost_postId_idx" ON "public"."Subpost"("postId"); +-- Optional if you list recent subposts per post frequently: +-- CREATE INDEX "Subpost_postId_createdAt_desc_idx" ON "public"."Subpost"("postId", "createdAt" DESC);If it’s already applied, create a new migration with the index statements instead.
12-13
: Validate delete semantics (Restrict vs Cascade).
ON DELETE RESTRICT
blocks deleting a Post with children. If product expects parent deletion to also remove subposts, switch toON DELETE CASCADE
and mirror it in the Prisma schema (onDelete: Cascade
). Otherwise, keep as-is.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
prisma/migrations/20250915081725_add_subpost_model/migration.sql
(1 hunks)prisma/schema.prisma
(2 hunks)
🔇 Additional comments (3)
prisma/schema.prisma (3)
19-19
: No-op whitespace change.Nothing to do here.
61-61
: Relation field looks good; watch for N+1 at call sites.The
subposts: Subpost[]
addition is correct. Ensure query layers load subposts viainclude
or a batched loader to avoid N+1.
67-74
: Resolved — Post.title already created in migrations.prisma/migrations/20250904104634_add_post_model/migration.sql creates the Post table with "title" TEXT NOT NULL (lines 5–9), so no separate title backfill/migration is required.
…ds and restructuring
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
🧹 Nitpick comments (1)
prisma/schema.prisma (1)
61-73
: Make FK actions explicit and index postId; consider restoring array GIN indexes.Explicitly align referential actions with the migration and add an index for join performance. Re-add GIN indexes if you query array containment.
model Subpost { id String @id @default(uuid()) content String sentiment Sentiment source Source categories String[] @default([]) subcategories String[] @default([]) link String? postId String - post Post @relation(fields: [postId], references: [id]) + post Post @relation(fields: [postId], references: [id], onDelete: Restrict, onUpdate: Cascade) createdAt DateTime @default(now()) updatedAt DateTime @updatedAt + @@index([postId]) + // If you frequently query by array containment, consider: + // @@index([categories], type: Gin) + // @@index([subcategories], type: Gin) }If you actually want parent deletion to remove subposts, switch
onDelete: Cascade
. Confirm desired behavior.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
prisma/schema.prisma
(2 hunks)
🔇 Additional comments (1)
prisma/schema.prisma (1)
19-19
: No-op change on User.email — OK to keep.Semantics unchanged; no action needed.
…d adding new fields
Description:
This PR updates the Prisma schema to support hierarchical content by:
Subpost
model for child entries under a post.Post
andSubpost
(each post can have multiple subposts).Post
model includes atitle
field.createdAt
,updatedAt
) for both models.These changes enable posts to have associated subposts, supporting threaded or nested content structures.
Summary by CodeRabbit
New Features
Chores