© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago
Turkish

Querying based on record in another table

I have this schema:
export const users = pgTable("users", {
  id: serial("id").primaryKey(),
  name: text("name"),
});
export const usersRelations = relations(users, ({ many }) => ({
  posts: many(posts),
  seenPosts: many(seenPosts),
}));
export type User = InferSelectModel<typeof users>;
export type UserWithPosts = User & { posts: Post[] };

export const posts = pgTable("posts", {
  id: serial("id").primaryKey(),
  userId: integer("user_id").notNull(),
  content: text("content"),
});
export const postsRelations = relations(posts, ({ one, many }) => ({
  author: one(users, {
    fields: [posts.userId],
    references: [users.id],
  }),
  seenPosts: many(seenPosts),
}));
export type Post = InferSelectModel<typeof posts>;
export type PostWithAuthor = Post & { author: User };

export const seenPosts = pgTable("seen_posts", {
  userId: integer("user_id").references(() => users.id),
  postId: integer("post_id").references(() => posts.id),
});

export const seenPostsRelations = relations(seenPosts, ({ one }) => ({
  user: one(users, {
    fields: [seenPosts.userId],
    references: [users.id],
  }),
  post: one(posts, {
    fields: [seenPosts.postId],
    references: [posts.id],
  }),
}));
export type SeenPost = InferSelectModel<typeof seenPosts>;
export type SeenPostWithUserAndPost = SeenPost & {
  user: User;
  post: Post;
};
export const users = pgTable("users", {
  id: serial("id").primaryKey(),
  name: text("name"),
});
export const usersRelations = relations(users, ({ many }) => ({
  posts: many(posts),
  seenPosts: many(seenPosts),
}));
export type User = InferSelectModel<typeof users>;
export type UserWithPosts = User & { posts: Post[] };

export const posts = pgTable("posts", {
  id: serial("id").primaryKey(),
  userId: integer("user_id").notNull(),
  content: text("content"),
});
export const postsRelations = relations(posts, ({ one, many }) => ({
  author: one(users, {
    fields: [posts.userId],
    references: [users.id],
  }),
  seenPosts: many(seenPosts),
}));
export type Post = InferSelectModel<typeof posts>;
export type PostWithAuthor = Post & { author: User };

export const seenPosts = pgTable("seen_posts", {
  userId: integer("user_id").references(() => users.id),
  postId: integer("post_id").references(() => posts.id),
});

export const seenPostsRelations = relations(seenPosts, ({ one }) => ({
  user: one(users, {
    fields: [seenPosts.userId],
    references: [users.id],
  }),
  post: one(posts, {
    fields: [seenPosts.postId],
    references: [posts.id],
  }),
}));
export type SeenPost = InferSelectModel<typeof seenPosts>;
export type SeenPostWithUserAndPost = SeenPost & {
  user: User;
  post: Post;
};


In my nextjs api route (pages router), I cant figure out how should i query only the posts that does not have a record saved in the seenPosts table .

My query:

const unseenPosts = await db.query.posts.findMany({
    with: {
      author: true,
      seenPosts: {
        where({ userId }, { eq }) {
          return eq(userId, LOGGED_IN_USER_ID);
        },
      },
    },
  });
const unseenPosts = await db.query.posts.findMany({
    with: {
      author: true,
      seenPosts: {
        where({ userId }, { eq }) {
          return eq(userId, LOGGED_IN_USER_ID);
        },
      },
    },
  });


Any help is appreciated, many thanks ❤️
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

Querying table based on a field of a related table
Drizzle TeamDTDrizzle Team / help
3y ago
Filter based querying
Drizzle TeamDTDrizzle Team / help
2y ago
Query a table based on related table
Drizzle TeamDTDrizzle Team / help
3y ago
Typing columns based on Table
Drizzle TeamDTDrizzle Team / help
3y ago