© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
17 replies
brynthrax

How to flatten select results from query with left joins (one-to-many)?

Is there a way to achieve this? Current behavior in drizzle works like this (assuming we're printing the variable "result" which is supposed to be an array of person with all their hobbies, one to many:

// NOTE: I've read a portion of the docs and it says something like //using a reduce function. I'm looking for a solution that is more // of an abstraction that works out of the box - just like how
// things are when you use a relational query (i.e., findMany)
[
  {
    person: {
      id: "P1",
      name: "Jane Doe"
      ...
    }
    hobby: { id: "H1", ... }
  },
 {
    person: {
      id: "P1",
      name: "Jane Doe"
      ...
    }
    hobby: { id: "H2", ... }
  },
{
    person: {
      id: "P3",
      name: "Bob Smith"
      ...
    }
    hobby: { id: "H5", ... }
  }
]
// NOTE: I've read a portion of the docs and it says something like //using a reduce function. I'm looking for a solution that is more // of an abstraction that works out of the box - just like how
// things are when you use a relational query (i.e., findMany)
[
  {
    person: {
      id: "P1",
      name: "Jane Doe"
      ...
    }
    hobby: { id: "H1", ... }
  },
 {
    person: {
      id: "P1",
      name: "Jane Doe"
      ...
    }
    hobby: { id: "H2", ... }
  },
{
    person: {
      id: "P3",
      name: "Bob Smith"
      ...
    }
    hobby: { id: "H5", ... }
  }
]


What I want to achieve is an array wherein these relations are flattened, such as:
[
  {
    "id": "P1",
    "name": "Jane Doe"
    "hobby": [
      { "id": "H1", ...  },
      { "id": "H2", ...  }
    ],
    ...
  },
  {
    "id": "P2",
    "name": "Bob Smith"
    "hobby": [
      { "id": "H5", ...  }
    ],
    ...
  }
]
[
  {
    "id": "P1",
    "name": "Jane Doe"
    "hobby": [
      { "id": "H1", ...  },
      { "id": "H2", ...  }
    ],
    ...
  },
  {
    "id": "P2",
    "name": "Bob Smith"
    "hobby": [
      { "id": "H5", ...  }
    ],
    ...
  }
]
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

Many-to-Many joins results in weird values
Drizzle TeamDTDrizzle Team / help
3y ago
Many to many flatten response
Drizzle TeamDTDrizzle Team / help
2y ago
Query only one item from Many-To-Many
Drizzle TeamDTDrizzle Team / help
3y ago
one to many query
Drizzle TeamDTDrizzle Team / help
3y ago