Querying data
Hi,
Based on the schema below, I am trying to query all the capex items for a given document and render it in a <div>. Can anyone help me out with this?
export default defineSchema({
documents: defineTable({
title: v.string(),
userId: v.string(),
isArchived: v.boolean(),
isPublished: v.boolean(),
parentDocument: v.optional(v.id("documents")),
client: v.optional(v.string()),
country: v.optional(v.string()),
technology: v.string(),
description: v.optional(v.string()),
})
.index("by_user", ["userId"])
.index("by_user_parent", ["userId", "parentDocument"]),
// add a project table and make it a child of documents
capex: defineTable({
capexName: v.string(),
capexValue: v.float64(),
endDate: v.float64(),
startDate: v.float64(),
capexDescription: v.optional(v.string()),
userId: v.string(),
parentDocument: v.id("documents"),
})
.index("by_user", ["userId"])
.index("by_user_parent", ["userId", "parentDocument"]),
3 Replies
do you need userId in both records? if each capex is owned by a document than is the document's user the same as the capex record?
You are right. I do not need userId in the capex table. The document's user should be the same as the capex...
for querying based on relationships, check this out: https://stack.convex.dev/relationship-structures-let-s-talk-about-schemas
Relationship Structures: Let's Talk About Schemas
In this post we’ll look at some patterns for structuring relationships in the Convex database.