© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago•
1 reply
Nacho Elias

Many to many flatten response

I'm struggling to understand why does drizzle work this way and why there isnt an easy way to flatten the response structure. I have 3 tables, Profile, Interest and Language. I also have 2 other join tables, ProfileInterest and ProfileLanguage. As you may have noticed, I have a many-to-many relationship between Profile and Interest and between Profile and Language. All 5 tables are in place (3 "models" and 2 join tables). I also have all the relations created and everything works as expected. To query for a specific profile including its' interests and languages I run the following.

db.query.Profile.findFirst({
        where: eq(Profile.id, input.id),
        with: {
          interests: {
            with: {
              interest: true,
            },
          },
          languages: {
            with: {
              language: true,
            },
          },
        },
      });
db.query.Profile.findFirst({
        where: eq(Profile.id, input.id),
        with: {
          interests: {
            with: {
              interest: true,
            },
          },
          languages: {
            with: {
              language: true,
            },
          },
        },
      });


The problem? The response looks like this

const currentOutput = {
  ...Profile, // All the fields from Profile
  interests: [
    {
      profileId: 1,
      interestId: 1,
      interest: {
        id: 1,
        name: "Music",
      },
    },
  ],
  languages: [
    {
      profileId: 1,
      languageId: 1,
      language: {
        id: 1,
        name: "English",
        code: "en",
      },
    },
  ],
};
const currentOutput = {
  ...Profile, // All the fields from Profile
  interests: [
    {
      profileId: 1,
      interestId: 1,
      interest: {
        id: 1,
        name: "Music",
      },
    },
  ],
  languages: [
    {
      profileId: 1,
      languageId: 1,
      language: {
        id: 1,
        name: "English",
        code: "en",
      },
    },
  ],
};


And of course, I'd like the following instead
const desiredOutput = {
  ...Profile,
  interests: [
    {
      id: 1,
      name: "Music",
    },
  ],
  languages: [
    {
      id: 1,
      name: "English",
      code: "en",
    },
  ],
};
const desiredOutput = {
  ...Profile,
  interests: [
    {
      id: 1,
      name: "Music",
    },
  ],
  languages: [
    {
      id: 1,
      name: "English",
      code: "en",
    },
  ],
};


Is there a way to achieve this using the queryAPI? At the moment the only thing I can do is either use it this way, which is not the end of the world but very inconvenient, or I can just flatten the structure in my server, which is also quite inconvenient...
Drizzle TeamJoin
The official Discord for all Drizzle related projects, such as Drizzle ORM, Drizzle Kit, Drizzle Studio and more!
11,879Members
Resources
Was this page helpful?

Similar Threads

Recent Announcements

Similar Threads

How to flatten select results from query with left joins (one-to-many)?
Drizzle TeamDTDrizzle Team / help
3y ago
Many to Many to Many
Drizzle TeamDTDrizzle Team / help
12mo ago
many to many help
Drizzle TeamDTDrizzle Team / help
17mo ago
mysql2 many-to-many
Drizzle TeamDTDrizzle Team / help
3y ago