© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•12mo ago
Nelson Sousa

Can't use `where` clause?

Hi community, I don't know why, but I am not able to use the where clause.

export const packages = pgTable('package', {
  id: serial('id').primaryKey().notNull(),
});

export const promotions = pgTable('promotion', {
  id: serial('id').primaryKey().notNull(),
  isActive: boolean('is_active').notNull().default(false),
});

export const packagePromotions = pgTable(
  'package_promotion',
  {
    packageId: integer('package_id')
      .notNull()
      .references(() => packages.id),
    promotionId: integer('promotion_id')
      .notNull()
      .references(() => promotions.id),
  },
  (table) => ({
    pk: primaryKey({ columns: [table.packageId, table.promotionId] }),
  }),
);

export const packagesRelations = relations(packages, ({ many}) => ({
  packagePromotions: many(packagePromotions),
}));

export const promotionsRelations = relations(promotions, ({ many }) => ({
  packages: many(packagePromotions),
}));

export const packagePromotionsRelations = relations(
  packagePromotions,
  ({ one }) => ({
    package: one(packages, {
      fields: [packagePromotions.packageId],
      references: [packages.id],
    }),
    promotion: one(promotions, {
      fields: [packagePromotions.promotionId],
      references: [promotions.id],
    }),
  }),
);
export const packages = pgTable('package', {
  id: serial('id').primaryKey().notNull(),
});

export const promotions = pgTable('promotion', {
  id: serial('id').primaryKey().notNull(),
  isActive: boolean('is_active').notNull().default(false),
});

export const packagePromotions = pgTable(
  'package_promotion',
  {
    packageId: integer('package_id')
      .notNull()
      .references(() => packages.id),
    promotionId: integer('promotion_id')
      .notNull()
      .references(() => promotions.id),
  },
  (table) => ({
    pk: primaryKey({ columns: [table.packageId, table.promotionId] }),
  }),
);

export const packagesRelations = relations(packages, ({ many}) => ({
  packagePromotions: many(packagePromotions),
}));

export const promotionsRelations = relations(promotions, ({ many }) => ({
  packages: many(packagePromotions),
}));

export const packagePromotionsRelations = relations(
  packagePromotions,
  ({ one }) => ({
    package: one(packages, {
      fields: [packagePromotions.packageId],
      references: [packages.id],
    }),
    promotion: one(promotions, {
      fields: [packagePromotions.promotionId],
      references: [promotions.id],
    }),
  }),
);


Query:

    const result = await db.query.packages.findFirst({
      where: eq(packages.id, packageId),
      with: {
        packagePromotions: {
          with: {
            promotion: {
              where: eq(promotions.isActive, true),
            },
          },
        },
      },
    });
    const result = await db.query.packages.findFirst({
      where: eq(packages.id, packageId),
      with: {
        packagePromotions: {
          with: {
            promotion: {
              where: eq(promotions.isActive, true),
            },
          },
        },
      },
    });


However, the
where
where
clause inside
promotion
promotion
does not exist. The goal its to get only the actived promotions. What am I missing? 🤔 Thank you!
image.png
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

Can I use "where" clause on joins ?
Drizzle TeamDTDrizzle Team / help
2y ago
Dynamic where clause
Drizzle TeamDTDrizzle Team / help
3y ago
Missing nested where clause
Drizzle TeamDTDrizzle Team / help
2y ago
[Beginner] complex "where" clause
Drizzle TeamDTDrizzle Team / help
3y ago