© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•15mo ago•
1 reply
Anthony

Double 'where' clause in dynamic query overriding the original 'where' clause not combining

Hi I am using Drizzle to fetch some records:

    const query = db
        .select({
            // some fields
        })
        .from(leads)
        .innerJoin(participants, eq(participants.id, leads.participantId))
        .innerJoin(
            organizations,
            eq(organizations.id, participants.organizationId)
        )
        .innerJoin(campaigns, eq(campaigns.id, leads.campaignId))
        .leftJoin(agents, eq(agents.id, leads.agentId))
        .leftJoin(sites, eq(sites.id, leads.siteId))
        .leftJoin(studies, eq(studies.id, leads.studyId))
        .where((t) => {
            // ... a bunch of conditions
        })
        .orderBy(desc(leads.updatedAt))
        .$dynamic();
    const query = db
        .select({
            // some fields
        })
        .from(leads)
        .innerJoin(participants, eq(participants.id, leads.participantId))
        .innerJoin(
            organizations,
            eq(organizations.id, participants.organizationId)
        )
        .innerJoin(campaigns, eq(campaigns.id, leads.campaignId))
        .leftJoin(agents, eq(agents.id, leads.agentId))
        .leftJoin(sites, eq(sites.id, leads.siteId))
        .leftJoin(studies, eq(studies.id, leads.studyId))
        .where((t) => {
            // ... a bunch of conditions
        })
        .orderBy(desc(leads.updatedAt))
        .$dynamic();


and I want to do an additional level of filtering as well later in the function like so:

await query.where(inArray(participants.id, searchResults)))
await query.where(inArray(participants.id, searchResults)))

However these results now don't still have the original filters from the where clause in initial dynamic query? Is this intentional and how do I fix it?
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

Dynamic where clause
Drizzle TeamDTDrizzle Team / help
3y ago
Chaining/combining $dynamic query functions
Drizzle TeamDTDrizzle Team / help
3y ago
Dynamic where query
Drizzle TeamDTDrizzle Team / help
3y ago
Query where clause with array.
Drizzle TeamDTDrizzle Team / help
3y ago