© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•12mo ago•
4 replies
GT

Filter Empty Relations out

I got both tables as defined in my schema file:
export const employeesTable = table(
    'employees', {
    id: int().autoincrement().primaryKey(),
    fullName: varchar("full_name", { length: 255 }).notNull(),
    cellphone: varchar("cellphone", { length: 15 }).notNull(),
    cpf: varchar( { length: 14 }).notNull().unique(),
});

export const availabilityTable = table(
    'employees_availability', {
        id: int().autoincrement().primaryKey(),
        employeeId: int().notNull().references((): AnyMySqlColumn => employeesTable.id),
        day: int().notNull(),
        startTime: time().notNull(),
        endTime: time().notNull(),
    }
);

...
export const employeesTable = table(
    'employees', {
    id: int().autoincrement().primaryKey(),
    fullName: varchar("full_name", { length: 255 }).notNull(),
    cellphone: varchar("cellphone", { length: 15 }).notNull(),
    cpf: varchar( { length: 14 }).notNull().unique(),
});

export const availabilityTable = table(
    'employees_availability', {
        id: int().autoincrement().primaryKey(),
        employeeId: int().notNull().references((): AnyMySqlColumn => employeesTable.id),
        day: int().notNull(),
        startTime: time().notNull(),
        endTime: time().notNull(),
    }
);

...

How could I query all Employees that has availabilities?

An simple query as this one:
database.query.employeesTable.findMany({
  with: { availabilities: true}
});
database.query.employeesTable.findMany({
  with: { availabilities: true}
});

Would also return empty availabilities, filtering out the result after the query will result in inconsistencies in the result size. How could I resolve that?
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

Filter included relations
Drizzle TeamDTDrizzle Team / help
2y ago
Filter on included relations
Drizzle TeamDTDrizzle Team / help
14mo ago
Filter on relations based upon a WITH
Drizzle TeamDTDrizzle Team / help
3y ago
Nested where filter, how to not include empty
Drizzle TeamDTDrizzle Team / help
3y ago