© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
1 reply
cobite

How to query from a many to many relationship?

Hi all,

I am trying to get all the posts by a user.
My schema has a many-to-many relationship set up for posts and users.



export const posts = pgTable('posts', {
  id: uuid('id').primaryKey().defaultRandom().unique().notNull(),
});

export const postsRelations = relations(posts, ({ many }) => ({
  users: many(users),
}));

export type Post = InferModel<typeof posts>;
export const posts = pgTable('posts', {
  id: uuid('id').primaryKey().defaultRandom().unique().notNull(),
});

export const postsRelations = relations(posts, ({ many }) => ({
  users: many(users),
}));

export type Post = InferModel<typeof posts>;



And then:

export const users = pgTable('users', {
  id: uuid('id').primaryKey().defaultRandom().unique().notNull(),
});

export const usersRelations = relations(users, ({ many }) => ({
  posts: many(posts),
}));

export type User = InferModel<typeof users>;
export const users = pgTable('users', {
  id: uuid('id').primaryKey().defaultRandom().unique().notNull(),
});

export const usersRelations = relations(users, ({ many }) => ({
  posts: many(posts),
}));

export type User = InferModel<typeof users>;




Now in my file I want to

export default async function getAllUsersPosts(userId: string): Promise<Post[]> {
  const results = await queryDB.select().from(posts).where(eq(posts.users.id, userId));
  return results;
}
export default async function getAllUsersPosts(userId: string): Promise<Post[]> {
  const results = await queryDB.select().from(posts).where(eq(posts.users.id, userId));
  return results;
}


Error is:

Property 'users' does not exist on type 'PgTableWithColumns<{ name: "posts"; schema: undefined; columns: { id: PgColumn<{ name: "id"; tableName: "posts"; dataType: "string"; columnType: "PgUUID"; data: string; driverParam: string; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }
Property 'users' does not exist on type 'PgTableWithColumns<{ name: "posts"; schema: undefined; columns: { id: PgColumn<{ name: "id"; tableName: "posts"; dataType: "string"; columnType: "PgUUID"; data: string; driverParam: string; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }
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

update many to many relationship
Drizzle TeamDTDrizzle Team / help
11mo ago
How to query many-many with mysql
Drizzle TeamDTDrizzle Team / help
2y ago
Querying and typing a many to many relationship?
Drizzle TeamDTDrizzle Team / help
2y ago
Query only one item from Many-To-Many
Drizzle TeamDTDrizzle Team / help
3y ago