having trouble to include relations with query function

how do you exactly use the with field in the query options to include relations ?, my functions always returned referencedTable error my relation is declared like the following
export const eventsRelations = relations(events, ({ many, one }) => ({
host: one(users, {
fields: [events.createdBy],
references: [users.id],
}),
}));

export const usersRelations = relations(users, ({ many }) => ({
events: many(events),
}));
export const eventsRelations = relations(events, ({ many, one }) => ({
host: one(users, {
fields: [events.createdBy],
references: [users.id],
}),
}));

export const usersRelations = relations(users, ({ many }) => ({
events: many(events),
}));
and here's the query
async getEventBySlug(slug: string): Promise<selectEvent | undefined> {
const data = await db.query.events.findFirst({
with: { host: true },
where: and(eq(events.slug, slug), isNull(events.deletedAt)),
});

if (data) return data as selectEvent;
else return undefined;
}
async getEventBySlug(slug: string): Promise<selectEvent | undefined> {
const data = await db.query.events.findFirst({
with: { host: true },
where: and(eq(events.slug, slug), isNull(events.deletedAt)),
});

if (data) return data as selectEvent;
else return undefined;
}
i've also tried to change the with object to {users:true} but still receiving the referencedTable error
3 Replies
IgnisMurasaki
IgnisMurasakiOP10mo ago
am i missing something, is this relation variable should be assigned somewhere ? turns out yes it's, I didn't include the relation when creating the drizzle instance
piscopancer
piscopancer10mo ago
why do you reference createdAt in the relation? Do you use it as your primary key? If yes, then it's wrong, you should use id instead. Also, after you fixed it, how does your correct schema now looks like?
Want results from more Discord servers?
Add your server