Seeking Solace
Seeking Solace2mo ago

Push id of a newly created document to another on creation

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")),
}),
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;
},
});
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.",
}),
});
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?
4 Replies
Convex Bot
Convex Bot2mo ago
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 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!
erquhart
erquhart2mo ago
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.
erquhart
erquhart2mo ago
If you haven't already, I'd highly recommend trying out the tutorial, it walks through a lot of these concepts: https://docs.convex.dev/tutorial/
Convex Tutorial: A Chat App | Convex Developer Hub
Convex provides you with a fully featured backend with cloud functions,
Seeking Solace
Seeking SolaceOP2mo ago
That makes a lot of sense, thanks a lot for the valuable advice! I will be doing that.

Did you find this page helpful?