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>; 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>; 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; } 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; }
1 Reply
Liltripple_reid
Liltripple_reid17mo ago
you can maybe use the RQB instead like
const data = await queryDB.query.posts.findMany({
with: {
user: true
}
})
const data = await queryDB.query.posts.findMany({
with: {
user: true
}
})
Want results from more Discord servers?
Add your server