Defining a one-to-many relation within the same table?

I have a Postgres table phone_call and need to define a one-to-many relation between a row and other rows in the table:

export const phone_call = pgTable("phone_call", {
  id: text("id").primaryKey().notNull(),
  parent_id: text("parent_id"), // if null, the this is a parent, otherwise a child call
  owner_id: varchar("owner_id", { length: 20 }).notNull(),
 //...
});


export const phoneCallRelations = relations(schema.phone_call, ({ one, many }) => ({
  owner: one(schema.user, {
    fields: [schema.phone_call.owner_id],
    references: [schema.user.id],
  }),
  parent: one(schema.phone_call, {
    fields: [schema.phone_call.parent_id],
    references: [schema.phone_call.id],
  }),
  children: many(schema.phone_call),
}));


When I query findMany with parent, it works, but when I do a with children, it doesn't.

No problem with 1-to-M across tables... but within the same table is there a way to do this?

Much appreciated!
Was this page helpful?