one-to-many self reference

Hello guys,
this is a simplified section of my schema:
 typescript
export const contacts = mysqlTable("contacts", {
  id: int("id").primaryKey(),
  parentId: int("parent_id"),
  ...
}, (table) => {
  return {
    parentReference: foreignKey({
      columns: [table.parentId],
      foreignColumns: [table.id],
      name: "fk_contacts_parent_id"
    }),
  };
})

contacts are families or family members, where parent_id points to the family
I tried to update the above code:
  parentId: int('parent_id').references(() => contacts.id),

but I'm getting this error:
'contacts' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.ts(7022)

and also have this:
export const contactsRelations = relations(contacts, ({ many, one }) => ({
  contactCustomFields: many(contactCustomFields),
  parent: one(contacts, {
    fields: [contacts.parentId],
    references: [contacts.id],
  }),
  familyMembers: many(contacts),
})),

even without the .references, the below code silently fails:
const result = await db.query.contacts.findMany({
    with: {
      parent: true,
      familyMembers: true,
    },
    limit: 1
  })
Was this page helpful?