© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
Drizzle TeamDT
Drizzle Team•3y ago•
6 replies
rfrancociavaldini

Relations include query

For example If I have the following schema:

export const sales = mysqlTable("Sales", {
    id: int("id").autoincrement().notNull(),
    startingDate: datetime("starting_date", { mode: 'string'}).notNull(),
    expirationDate: datetime("expiration_date", { mode: 'string'}),
    bundleId: int("BundleId").references(() => bundles.id, { onDelete: "set null", onUpdate: "cascade" } ),
},
(table) => {
    return {
        bundleId: index("BundleId").on(table.bundleId),
        salesId: primaryKey(table.id),
    }
});

export const bundles = mysqlTable("Bundles", {
    id: int("id").autoincrement().notNull(),
    amount: float("amount").notNull(),
    name: varchar("name", { length: 255 }),
},
(table) => {
    return {
        bundlesId: primaryKey(table.id),
    }
});
export const sales = mysqlTable("Sales", {
    id: int("id").autoincrement().notNull(),
    startingDate: datetime("starting_date", { mode: 'string'}).notNull(),
    expirationDate: datetime("expiration_date", { mode: 'string'}),
    bundleId: int("BundleId").references(() => bundles.id, { onDelete: "set null", onUpdate: "cascade" } ),
},
(table) => {
    return {
        bundleId: index("BundleId").on(table.bundleId),
        salesId: primaryKey(table.id),
    }
});

export const bundles = mysqlTable("Bundles", {
    id: int("id").autoincrement().notNull(),
    amount: float("amount").notNull(),
    name: varchar("name", { length: 255 }),
},
(table) => {
    return {
        bundlesId: primaryKey(table.id),
    }
});


I'm trying the following query and it throws me an error:

await db.query.sales.findFirst({
    where: eq(sales.businessUserId, id),
    orderBy: desc(sales.id),
    with: {
        bundles: true
    },
  });
await db.query.sales.findFirst({
    where: eq(sales.businessUserId, id),
    orderBy: desc(sales.id),
    with: {
        bundles: true
    },
  });


The error is
undefined is not an object (evaluating 'relation.referencedTable')
undefined is not an object (evaluating 'relation.referencedTable')
.

How can I get a
Sale
Sale
object with it's
Bundle
Bundle
complete object instead of only the
BundleId
BundleId
? To avoid having to do another query.
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

having trouble to include relations with query function
Drizzle TeamDTDrizzle Team / help
2y ago
Include count of relations
Drizzle TeamDTDrizzle Team / help
3y ago
Drizzle-zod Include Relations
Drizzle TeamDTDrizzle Team / help
3y ago
Querying relations
Drizzle TeamDTDrizzle Team / help
2y ago