query one-to-many with leftJoin #1658
-
I have an article table which has a one-to-many relation to another table called const articles = await db
.select()
.from(schema.article)
.leftJoin(
schema.heading,
eq(schema.article.id, schema.heading.articleId),
)
.orderBy(
desc(schema.article.publishedAt),
desc(schema.article.title),
)
.where(...)
.offset((page - 1) * LIMIT)
.limit(LIMIT); the problem is that the PS. I am using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 3 replies
-
What is in your where? If you are filtering only on stuff from your article, this is a solution: const articlesSQ = db
.select()
.from(schema.article)
.orderBy(
desc(schema.article.publishedAt),
desc(schema.article.title),
)
.where(...)
.offset((page - 1) * LIMIT)
.limit(LIMIT)
.as("articlesSQ")
const articles = await db
.select()
.from(articlesSQ)
.leftJoin(
schema.heading,
eq(articleSQ.id, schema.heading.articleId)
); You might need to add the selected columns to your select for this to work properly. I didn't test. |
Beta Was this translation helpful? Give feedback.
What is in your where? If you are filtering only on stuff from your article, this is a solution:
You might need to add the selected columns to your select for this to work properly. I didn't test.