© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago
r9d7

Relations between tables unclear

Consider the following schema where a User can create a Post, and each post can have multiple versions of its content. What is the best way for a Post to store the
latestVersionId
latestVersionId
for quick access. I'm having trouble figuring that out, and also how to create a Post since a Post would need a PostVersion first, but a PostVersion needs a Post to link them together.

export const posts = pgTable("posts", {
  id: serial("id").primaryKey(),

  authorId: integer("author_id").notNull(),
  parentPostId: integer("parent_post_id"),
  latestVersionId: integer("latest_version_id") // Is this the best way to store a reference to the latest post_version?
    .notNull() // It doesn't make sense for the latestVersionId to be null, so I set it as notNull. But in this case, how can I create a Post or a PostVersion? Either of these need the other during their creation
    .references(() => recordVersions.id),
});

export const postsRelations = relations(posts, ({ one, many }) => ({
  author: one(users, {
    fields: [records.authorId],
    references: [users.id],
  }),
  parentPost: one(posts, {
    fields: [posts.parentPostId],
    references: [posts.id],
  }),
  versions: many(postVersions),
}));

export const postVersions = pgTable("post_versions", {
  id: serial("id").primaryKey(),

  authorId: integer("author_id").notNull(),
  postId: integer("postId").notNull(),

  content: jsonb("content"),
});

export const postVersionsRelations = relations(postVersions, ({ one }) => ({
  author: one(users, {
    fields: [postVersions.authorId],
    references: [users.id],
  }),
  post: one(posts, {
    fields: [postVersions.postId],
    references: [posts.id],
  }),
}));
export const posts = pgTable("posts", {
  id: serial("id").primaryKey(),

  authorId: integer("author_id").notNull(),
  parentPostId: integer("parent_post_id"),
  latestVersionId: integer("latest_version_id") // Is this the best way to store a reference to the latest post_version?
    .notNull() // It doesn't make sense for the latestVersionId to be null, so I set it as notNull. But in this case, how can I create a Post or a PostVersion? Either of these need the other during their creation
    .references(() => recordVersions.id),
});

export const postsRelations = relations(posts, ({ one, many }) => ({
  author: one(users, {
    fields: [records.authorId],
    references: [users.id],
  }),
  parentPost: one(posts, {
    fields: [posts.parentPostId],
    references: [posts.id],
  }),
  versions: many(postVersions),
}));

export const postVersions = pgTable("post_versions", {
  id: serial("id").primaryKey(),

  authorId: integer("author_id").notNull(),
  postId: integer("postId").notNull(),

  content: jsonb("content"),
});

export const postVersionsRelations = relations(postVersions, ({ one }) => ({
  author: one(users, {
    fields: [postVersions.authorId],
    references: [users.id],
  }),
  post: one(posts, {
    fields: [postVersions.postId],
    references: [posts.id],
  }),
}));
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

Unable to infer relationship between tables
Drizzle TeamDTDrizzle Team / help
2y ago
Explaining the differences between .references() and relations function
Drizzle TeamDTDrizzle Team / help
3y ago
Can't create relation between two tables
Drizzle TeamDTDrizzle Team / help
16mo ago
Error: There are multiple relations between "__public__.shareTransferHistory" and "client".
Drizzle TeamDTDrizzle Team / help
2y ago