#Push id of a newly created document to another on creation

1 messages · Page 1 of 1 (latest)

fallen gulch
#

My schema looks like this:

  boards: defineTable({
    title: v.string(),
    users: v.array(v.id("users")),
    columns: v.array(v.id("columns")),
  }),
  columns: defineTable({
    title: v.string(),
    tasks: v.array(v.id("tasks")),
  }),

My createColumn mutation:

export const createColumn = mutation({
  args: zodToConvex(CreateColumnSchema),
  handler: async (ctx, args) => {
    const newColumnId = await ctx.db.insert("columns", { title: args.title, tasks: [] });
    // WIP: Update the respective board's columns
    return newColumnId;
  },
});

The Zod schema looks like this:

export const CreateColumnSchema = z.object({
  id: z.string(), // id of the board
  title: z.string().min(1, { message: "Title is required." }).max(32, {
    message: "Title is too long.",
  }),
});

I would like to push the id of the new column to the respective board document. How can I do this?

coarse crescentBOT
#

Thanks for posting in #1088161997662724167.
Reminder: If you have a Convex Pro account, use the Convex Dashboard to file support tickets.

    - Provide context: What are you trying to achieve, what is the end-user interaction, what are you seeing? (full error message, command output, etc.)
    - Use [search.convex.dev](https://search.convex.dev) to search Docs, Stack, and Discord all at once.
    - Additionally, you can post your questions in the Convex Community's #1228095053885476985 channel to receive a response from AI.
    - Avoid tagging staff unless specifically instructed.

    Thank you!
sharp ridge
#

Your mutation doesn't seem to align with your schema (inserting without the board id field, and tasks isn't in the schema), so I may be missing something. But the basic answer is you'd patch the board right where your // WIP comment is using ctx.db.patch.

That said, you're likely better off just keeping the board id on the columns (I'd call it boardId instead of just id), and then querying all columns for a given board id when you need them. Generally no need to store the column ids on the board unless you have a really particular use case.