© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
19 replies
Mr.Propre

relation select query with where condition

I want to select all user's tasks where parent id is null.
Can someone help me fix this query please:
const result = await db.query.usersTasks.findMany({
    with: {
      tasks: {
        with: {
          subtasks: true,
        },
        where: isNull(tasks.parentId),
      },
    },
    where: eq(usersTasks.userId, id),
  });
const result = await db.query.usersTasks.findMany({
    with: {
      tasks: {
        with: {
          subtasks: true,
        },
        where: isNull(tasks.parentId),
      },
    },
    where: eq(usersTasks.userId, id),
  });

these are the tables in question :
export const users = mysqlTable("users", {
  userId: varchar("userId", { length: 256 }).primaryKey().notNull().unique(),
  theme: mysqlEnum("theme", ["DARK", "LIGHT"]).default("DARK"),
  createdAt: timestamp("created_at").default(sql`CURRENT_TIMESTAMP`),
  updatedAt: timestamp("updated_at").default(sql`CURRENT_TIMESTAMP`),
});
export const usersTasks = mysqlTable(
  "users_tasks",
  {
    id: int("id").primaryKey().autoincrement(),
    userId: varchar("user_id", { length: 256 }).references(() => users.userId),
    taskId: int("tas_id").references(() => tasks.id),
    createdAt: timestamp("created_at").default(sql`CURRENT_TIMESTAMP`),
    updatedAt: timestamp("updated_at").default(sql`CURRENT_TIMESTAMP`),
  }
);
export const tasks = mysqlTable(
  "tasks",
  {
    id: int("id").primaryKey().autoincrement(),
    parentId: int("parent_id").references((): AnyMySqlColumn => tasks.id),
    title: varchar("title", { length: 256 }).notNull(),
    description: varchar("description", { length: 2000 }),
    dueDate: datetime("due_date"),
    createdAt: timestamp("created_at").default(sql`CURRENT_TIMESTAMP`),
    updatedAt: timestamp("updated_at").default(sql`CURRENT_TIMESTAMP`),
  },
  (table) => ({
    parentIdx: index("parent_idx").on(table.parentId)
  })
);

export const usersRelations = relations(users, ({ many }) => ({
  userTasks: many(usersTasks)
}));
export const tasksRelations = relations(tasks, ({ many, one }) => ({
  userTasks: many(usersTasks),
  subtasks: many(tasks, { relationName: "subtasks" }),
  parent: one(tasks)
}));
export const users = mysqlTable("users", {
  userId: varchar("userId", { length: 256 }).primaryKey().notNull().unique(),
  theme: mysqlEnum("theme", ["DARK", "LIGHT"]).default("DARK"),
  createdAt: timestamp("created_at").default(sql`CURRENT_TIMESTAMP`),
  updatedAt: timestamp("updated_at").default(sql`CURRENT_TIMESTAMP`),
});
export const usersTasks = mysqlTable(
  "users_tasks",
  {
    id: int("id").primaryKey().autoincrement(),
    userId: varchar("user_id", { length: 256 }).references(() => users.userId),
    taskId: int("tas_id").references(() => tasks.id),
    createdAt: timestamp("created_at").default(sql`CURRENT_TIMESTAMP`),
    updatedAt: timestamp("updated_at").default(sql`CURRENT_TIMESTAMP`),
  }
);
export const tasks = mysqlTable(
  "tasks",
  {
    id: int("id").primaryKey().autoincrement(),
    parentId: int("parent_id").references((): AnyMySqlColumn => tasks.id),
    title: varchar("title", { length: 256 }).notNull(),
    description: varchar("description", { length: 2000 }),
    dueDate: datetime("due_date"),
    createdAt: timestamp("created_at").default(sql`CURRENT_TIMESTAMP`),
    updatedAt: timestamp("updated_at").default(sql`CURRENT_TIMESTAMP`),
  },
  (table) => ({
    parentIdx: index("parent_idx").on(table.parentId)
  })
);

export const usersRelations = relations(users, ({ many }) => ({
  userTasks: many(usersTasks)
}));
export const tasksRelations = relations(tasks, ({ many, one }) => ({
  userTasks: many(usersTasks),
  subtasks: many(tasks, { relationName: "subtasks" }),
  parent: one(tasks)
}));
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

query where condition on foreign key relation
Drizzle TeamDTDrizzle Team / help
14mo ago
Select with relation
Drizzle TeamDTDrizzle Team / help
3y ago
Select where one->many relation has at least one matching a condition
Drizzle TeamDTDrizzle Team / help
3y ago
Help with query, how to use parent value in where condition ?
Drizzle TeamDTDrizzle Team / help
3y ago