© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•2y ago•
19 replies
Melvin

Querying relations

I have this schema:

const users = pgTable()...
const groups = pgTable()...
const usersToGroups = pgTable()....references the respective columns

const usersRelations = relations()...users have many groups
const groupsRelations = relations()...groups have many users

const usersToGroupsRelations = relations()...one userId to one groupId in join table
const users = pgTable()...
const groups = pgTable()...
const usersToGroups = pgTable()....references the respective columns

const usersRelations = relations()...users have many groups
const groupsRelations = relations()...groups have many users

const usersToGroupsRelations = relations()...one userId to one groupId in join table


Now I want to search for all the users in a group, so I write a query:

const filters: SQL[] = [];

query?.groupId ? filters.push(eq(groups.groupId, query.groupId)) : undefined;
query?.userId ? filters.push(eq(users.userId, query.userId)) : undefined;

const users = await db.query.users.findMany({
  columns: { 
    id: true,
    name: true,
  },
  with: {
    groups: {
      groupId: true,
      userId: true,
    }
    }
  },
  where: and(...filters),
)
const filters: SQL[] = [];

query?.groupId ? filters.push(eq(groups.groupId, query.groupId)) : undefined;
query?.userId ? filters.push(eq(users.userId, query.userId)) : undefined;

const users = await db.query.users.findMany({
  columns: { 
    id: true,
    name: true,
  },
  with: {
    groups: {
      groupId: true,
      userId: true,
    }
    }
  },
  where: and(...filters),
)


When I run this, I get the following error

ERROR [ExceptionsHandler] column users.group_id does not exist
error: column users.group_id does not exist
ERROR [ExceptionsHandler] column users.group_id does not exist
error: column users.group_id does not exist


What am I doing wrong? I'm explicitly selecting the columns I want and the generated sql looked right to me and I believe I've defined the relations correctly. Any help is greatly appreciated.
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

Problems querying with findMany and relations
Drizzle TeamDTDrizzle Team / help
11mo ago
Issue querying with relations in bun+sqlite
Drizzle TeamDTDrizzle Team / help
3y ago
Querying Views
Drizzle TeamDTDrizzle Team / help
2y ago
Disambiguating relations
Drizzle TeamDTDrizzle Team / help
12mo ago