I cant get Drizzle with Queries to work...

I'm converting a project from prisma, and I'm a bit stuck on the simplest of queries... This returns records:
await db.query.Session.findMany({})
await db.query.Session.findMany({})
while this:
await db.query.Session.findMany({
with: {User: true}
})
await db.query.Session.findMany({
with: {User: true}
})
throws TypeError: Cannot read properties of undefined (reading 'referencedTable') The Session table:
export const Session = pgTable("Session", {
sessionToken: text("sessionToken").notNull(),
userId: text("userId").notNull().references(() => User.id, { onDelete: "cascade", onUpdate: "cascade" } ),...
export const Session = pgTable("Session", {
sessionToken: text("sessionToken").notNull(),
userId: text("userId").notNull().references(() => User.id, { onDelete: "cascade", onUpdate: "cascade" } ),...
The User table:
export const User = pgTable("User", {
id: text("id").primaryKey().notNull(),
...
export const User = pgTable("User", {
id: text("id").primaryKey().notNull(),
...
(there was also a relations.ts file created during introspection that is not imported anywhere) I'm sure I've just missed something in the documentation, but I've been stuck on this for almost a day now.
1 Reply
thebjorn
thebjorn4mo ago
PS: I can get the records using .leftJoin, but the with: syntax seemed like an easier transition path from prisma's include: syntax... Nevermind, figured it out. I needed to initialize db with both schema and relations:
import * as schema from './schema.js'
import * as relations from './relations.js'
const client = postgres(process.env.POSTGRES_URL)

export const db = drizzle(client, {
schema: {
...schema,
...relations,
}
});
import * as schema from './schema.js'
import * as relations from './relations.js'
const client = postgres(process.env.POSTGRES_URL)

export const db = drizzle(client, {
schema: {
...schema,
...relations,
}
});
Want results from more Discord servers?
Add your server