db.query throwing undefined is not an object (evaluating 'relation.referencedTable')

Hi, so i have this schema called product.ts
import { pgTable, timestamp, uuid, varchar } from "drizzle-orm/pg-core";
import { categories } from "./category";
import { relations } from "drizzle-orm";

export const products = pgTable("products", {
id: uuid("id").primaryKey().defaultRandom(),
name: varchar("name", { length: 255 }),
categoryId: uuid("category_id").notNull().references(() => categories.id),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").defaultNow(),
deletedAt: timestamp("deleted_at"),
})

export const productsRelations = relations(products, ({ one }) => ({
category: one(categories,{
fields: [products.categoryId],
references: [categories.id],
})
}))

export type Product = typeof products.$inferSelect
export type NewProduct = typeof products.$inferInsert
import { pgTable, timestamp, uuid, varchar } from "drizzle-orm/pg-core";
import { categories } from "./category";
import { relations } from "drizzle-orm";

export const products = pgTable("products", {
id: uuid("id").primaryKey().defaultRandom(),
name: varchar("name", { length: 255 }),
categoryId: uuid("category_id").notNull().references(() => categories.id),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").defaultNow(),
deletedAt: timestamp("deleted_at"),
})

export const productsRelations = relations(products, ({ one }) => ({
category: one(categories,{
fields: [products.categoryId],
references: [categories.id],
})
}))

export type Product = typeof products.$inferSelect
export type NewProduct = typeof products.$inferInsert
and it's related to category with one-to-many relationship
import { relations } from "drizzle-orm";
import { pgTable, timestamp, uniqueIndex, uuid, varchar } from "drizzle-orm/pg-core";
import { products } from "./product";

export const categories = pgTable("categories", {
id: uuid("id").primaryKey().defaultRandom(),
name: varchar("name", { length: 255 }),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").defaultNow(),
}, (category) => {
return {
nameIndex: uniqueIndex("name_index").on(category.name),
}
})

export const categoriesRelations = relations(categories, ({ many }) => ({
products: many(products)
}))

export type Category = typeof categories.$inferSelect
export type NewCategory = typeof categories.$inferInsert
import { relations } from "drizzle-orm";
import { pgTable, timestamp, uniqueIndex, uuid, varchar } from "drizzle-orm/pg-core";
import { products } from "./product";

export const categories = pgTable("categories", {
id: uuid("id").primaryKey().defaultRandom(),
name: varchar("name", { length: 255 }),
createdAt: timestamp("created_at").notNull().defaultNow(),
updatedAt: timestamp("updated_at").defaultNow(),
}, (category) => {
return {
nameIndex: uniqueIndex("name_index").on(category.name),
}
})

export const categoriesRelations = relations(categories, ({ many }) => ({
products: many(products)
}))

export type Category = typeof categories.$inferSelect
export type NewCategory = typeof categories.$inferInsert
But when i try to do query findMany, it throws an error
await db.query.categories.findMany({
with: {
products: true,
},
});
await db.query.categories.findMany({
with: {
products: true,
},
});
The error like in the image. Thank you!
No description
5 Replies
Mykhailo
Mykhailo9mo ago
Hello, @NyxSR! Could you please show how do you initialize db? You might forget to pass schema parameter. https://orm.drizzle.team/docs/rqb
Drizzle ORM - Query
Drizzle ORM is a lightweight and performant TypeScript ORM with developer experience in mind.
No description
NyxSR
NyxSROP9mo ago
So i've created a file called db.ts
// db.ts
import "dotenv/config";
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from "postgres";
import * as schema from '@/db/schema'

const postgresURI: string = process.env.POSTGRES_URI!

export const sql = postgres(postgresURI, {max: 1})

export const db = drizzle(sql, { schema });
// db.ts
import "dotenv/config";
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from "postgres";
import * as schema from '@/db/schema'

const postgresURI: string = process.env.POSTGRES_URI!

export const sql = postgres(postgresURI, {max: 1})

export const db = drizzle(sql, { schema });
NyxSR
NyxSROP9mo ago
and for the schema value inside db variables is from this. what inside index.ts from db/schema is just a simple file
// db/schema/index.ts
export { users } from "./users";
export { categories } from "./category";
export { products } from "./product"
// db/schema/index.ts
export { users } from "./users";
export { categories } from "./category";
export { products } from "./product"
No description
Mykhailo
Mykhailo9mo ago
@NyxSR you have to update your exports because you need relations too and then you can import from your schema/index
export * from './users';
// other exports
export * from './users';
// other exports
NyxSR
NyxSROP9mo ago
Ahh i see, let me try that Thanks a lot @Mykhailo , that works !
Want results from more Discord servers?
Add your server