© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
4 replies
Sam

Explaining the differences between .references() and relations function

Hi I'm a bit unclear in the example why the comments table in this example does not use the .references() function on the author id field. Is this because the .relations() method and relations({one}) are achieveing the same thing but two different approaches?

https://orm.drizzle.team/docs/rqb#one-to-many

...
 
export const posts = pgTable('posts', {
    id: serial('id').primaryKey(),
    content: text('content'),
    authorId: integer('author_id'),
});
 
export const postsRelations = relations(posts, ({ one, many }) => ({
    author: one(users, {
        fields: [posts.authorId],
        references: [users.id],
    }),
    comments: many(comments)
}));
 
export const comments = pgTable('comments', {
    id: serial('id').primaryKey(),
    text: text('text'),
    authorId: integer('author_id'),
    postId: integer('post_id'),
});
 
export const commentsRelations = relations(comments, ({ one }) => ({
    post: one(posts, {
        fields: [comments.postId],
        references: [posts.id],
    }),
}));
...
 
export const posts = pgTable('posts', {
    id: serial('id').primaryKey(),
    content: text('content'),
    authorId: integer('author_id'),
});
 
export const postsRelations = relations(posts, ({ one, many }) => ({
    author: one(users, {
        fields: [posts.authorId],
        references: [users.id],
    }),
    comments: many(comments)
}));
 
export const comments = pgTable('comments', {
    id: serial('id').primaryKey(),
    text: text('text'),
    authorId: integer('author_id'),
    postId: integer('post_id'),
});
 
export const commentsRelations = relations(comments, ({ one }) => ({
    post: one(posts, {
        fields: [comments.postId],
        references: [posts.id],
    }),
}));
Drizzle Queries - Drizzle ORM
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
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

Relations with multiple fields and references, or with constraints.
Drizzle TeamDTDrizzle Team / help
3y ago
Relations between tables unclear
Drizzle TeamDTDrizzle Team / help
2y ago
TypeScript and Runtime Error when using string constants in relations references array
Drizzle TeamDTDrizzle Team / help
6mo ago
Error: There are multiple relations between "__public__.shareTransferHistory" and "client".
Drizzle TeamDTDrizzle Team / help
2y ago