© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
8 replies
SPS | Shootmail

Can't figure out how to design relational query

I have three tables
- categories
- products
- media

I want to retrieve products that belong to a particular category, along with the product category and all images. The results should be paginated and should be filtered by category_slug, organization_id.

Relationships:
1. Many products can belong to one category (products - categories = Many-to-one)
2. Many products can have many images (products - images = Many-to-Many)

I have tried:
const productsQuery = db.query.categories.findMany({
            with: {
                productsOnCategory: {
                    with: {
                        mediaOnProducts: {
                            with: { media: true }
                        }
                    },
                    where: (products, {and, eq}) => and(eq(products.isVisible, 1), eq(products.organizationId, orgId))
                }
            },
            where: (categories, { and }) => and(eq(categories.slug, slug), eq(categories.isVisible, 1), eq(categories.organizationId, orgId))
        });
const productsQuery = db.query.categories.findMany({
            with: {
                productsOnCategory: {
                    with: {
                        mediaOnProducts: {
                            with: { media: true }
                        }
                    },
                    where: (products, {and, eq}) => and(eq(products.isVisible, 1), eq(products.organizationId, orgId))
                }
            },
            where: (categories, { and }) => and(eq(categories.slug, slug), eq(categories.isVisible, 1), eq(categories.organizationId, orgId))
        });

Problem: Cannot paginate the products using limit and offset as offset is only available for the top level relation which here is categories.

db.query.products.findMany({
            with: {
                category: {
                    
                }
            }
  });
db.query.products.findMany({
            with: {
                category: {
                    
                }
            }
  });

Problem: Cannot filter on the basis of category_slug since
where
where
is not available inside nested
category
category
I guess because this is a Many-to-one relation.

How can achieve the result I want using the relation queries?
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

Relational Query - how to call mapRelationalRow
Drizzle TeamDTDrizzle Team / help
3y ago
How to derive type from relational query?
Drizzle TeamDTDrizzle Team / help
3y ago
Nullable relational query?
Drizzle TeamDTDrizzle Team / help
3y ago
Relational query problem
Drizzle TeamDTDrizzle Team / help
3y ago