I need help with simple Table design 🥺

Hello! I've been trying to make a simple category-images relation work for past two days and I keep running into cannot infer relation errors. I finally have a working schema: export const categories = pgTable("categories", { id: text("id").primaryKey().unique(), parentId: text("parent_id").notNull() }); export const images = pgTable("images", { id: text("id").primaryKey().unique(), categoryId: text("category_id") .notNull().references(() => categories.id), } and relations: export const categoriesRelations = relations(categories, ({ one, many }) => ({ parent: one(categories, { relationName: "children", fields: [categories.parentId], references: [categories.id] }), children: many(categories, { relationName: "children" }), images: many(images, { relationName: "images" }), })); export const imagesRelations = relations(images, ({ one }) => ({ category: one(categories, { fields: [images.categoryId], references: [categories.id], relationName: "images" }), })); however I'm only able to query images that are directly related to categories. what I'd like to do is to be able to query category "all" and get images that are nested deep in the category three, like image with a relation to "lion" category, given that category three looks like this: "/all/nature/animals/lion". All tips are appreciated 🙏
1 Reply
tzezar
tzezar10mo ago
it is better practice to store foreign keys as integer not text and about error: please provide error message
Want results from more Discord servers?
Add your server