Currently, I am working with two schemas named products.ts
and category.ts
. The relationship between these files is defined as one-to-many.
In the products.ts
file:
import { pgTable, timestamp, uuid, varchar } from "drizzle-orm/pg-core";
import { categories } from "./category";
import { relations } from "drizzle-orm";
// Code snippet defining the products table...
export const productsRelations = relations(products, ({ one }) => ({
category: one(categories,{
fields: [products.categoryId],
references: [categories.id],
})
}))
// Additional type definitions related to products...
In the category.ts
file:
import { relations } from "drizzle-orm";
import { pgTable, timestamp, uniqueIndex, uuid, varchar } from "drizzle-orm/pg-core";
import { products } from "./product";
// Code snippet defining the categories table...
export const categoriesRelations = relations(categories, ({ many }) => ({
products: many(products)
}))
// Additional type definitions related to categories...
However, an error occurs when attempting to run the query findMany on the categories table:
await db.query.categories.findMany({
with: {
products: true,
},
});
https://i.sstatic.net/H4ht7.png
I would appreciate any assistance in resolving this issue. The desired outcome is to have the data retrieved by the query displayed as follows:
type CategoryWithProducts = {
id: string;
name: string | null;
createdAt: Date;
updatedAt: Date | null;
products: Products[];
}