Skip to content

v1.1.2 Not able to use update() to add entry into many() relation #346

@heiwen

Description

@heiwen

In the following example, the final post only has one (1) comment, whereas I would expect it to have two (2).

const postSchema = z.object({
  get comments() {
    return z.array(commentSchema)
  },
})
const commentSchema = z.object({
  text: z.string(),
})

const posts = new Collection({ schema: postSchema })
const comments = new Collection({ schema: commentSchema })

posts.defineRelations(({ many }) => ({
  comments: many(comments),
}))

const post = await posts.create({ 
  comments: [await comments.create({ text: 'First!' })]
});

const comment2 = await comments.create({ text: 'Second!' });

const updatedPost = await posts.update(post, {
  data(p) {
    p.comments = [  ...post.comments, comment2 ];
    // p.comments.push(comment2) also doesn't work
  }})

console.log(updatedPost.comments.length)

I'm using Bun 1.3.2, msw 2.12.2, msw/data 1.1.2 and zod 4.1.12.

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedExtra attention is needed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions