Skip to content

Self Referencing Relationships Fail #363

@jrjohnson

Description

@jrjohnson

When creating self referencing relationship between models we're seeing failures across all relationship types.

Eg:

const userSchema = z.object({
  id: z.number(),
  get children() {
    return z.array(userSchema).optional()
  },
  get parent() {
    return userSchema.optional()
  },
})

const users = new Collection({ schema: userSchema })

users.defineRelations(({ one, many }) => ({
  children: many(users, { role: 'children' }),
  parent: one(users, { role: 'children' }),
}))

const child = await users.create({
  id: 1,
})
const parent = await users.create({
  id: 2,
  children: [child],
})

This should result in a child with a parent and a parent with one child. Instead you get a child with itself as children.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions