© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago•
1 reply
Anas Badran

Nested queries

Based on the provided schema, how to react the query to fetch first 25 posts a long with their comments and reactions, and each comment have it's own reaction, anyone have any ideas?
export const now = () => sql<Date>`now()`

export const post = pgTable('posts', {
    id: integer('id',).primaryKey().generatedByDefaultAsIdentity(),
    createdAt: timestamp('created_at').defaultNow().notNull(),
    updatedAt: timestamp('updated_at')
        .defaultNow()
        .$onUpdate(now),
    title: varchar('name', { length: 255 }).notNull(),
    content: text('content').notNull()
})


export const reaction = pgTable('reactions', {
    id: integer('id',).primaryKey().generatedByDefaultAsIdentity(),
    createdAt: timestamp('created_at').defaultNow().notNull(),
    type: ReactionType('type').notNull(),
    key: varchar('key', { length: 255 }).notNull()
})

export const comment = pgTable('comments', {
    id: integer('id',).primaryKey().generatedByDefaultAsIdentity(),
    createdAt: timestamp('created_at').defaultNow().notNull(),
    content: text('content').notNull(),
    postsID: integer('post_id').references(() => post.id, { onDelete: 'cascade' }).notNull()
})
export const now = () => sql<Date>`now()`

export const post = pgTable('posts', {
    id: integer('id',).primaryKey().generatedByDefaultAsIdentity(),
    createdAt: timestamp('created_at').defaultNow().notNull(),
    updatedAt: timestamp('updated_at')
        .defaultNow()
        .$onUpdate(now),
    title: varchar('name', { length: 255 }).notNull(),
    content: text('content').notNull()
})


export const reaction = pgTable('reactions', {
    id: integer('id',).primaryKey().generatedByDefaultAsIdentity(),
    createdAt: timestamp('created_at').defaultNow().notNull(),
    type: ReactionType('type').notNull(),
    key: varchar('key', { length: 255 }).notNull()
})

export const comment = pgTable('comments', {
    id: integer('id',).primaryKey().generatedByDefaultAsIdentity(),
    createdAt: timestamp('created_at').defaultNow().notNull(),
    content: text('content').notNull(),
    postsID: integer('post_id').references(() => post.id, { onDelete: 'cascade' }).notNull()
})
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

nested relation queries
Drizzle TeamDTDrizzle Team / help
3y ago
types from nested relational queries
Drizzle TeamDTDrizzle Team / help
3y ago
Nested "with" queries infer type "any"
Drizzle TeamDTDrizzle Team / help
2y ago
Nested where clauses in relational queries
Drizzle TeamDTDrizzle Team / help
3y ago