andylukak
andylukak16mo ago

schema help

I read the documentation regarding schema declarations, but I do have one problem. I have a table called  people and has name, address. I have another table called images which are linked to the people’s ids. Is it any possibility for me to Quarry people and get all the images associated with them? I’m assuming there has to be something to put in the people schema, but I couldn’t quite figure it out. 
3 Replies
Bogdan
Bogdan16mo ago
if the foreign key in the images table is the images primary key, you could use indexes
images: defineTable({
userId: v.id("users"),
...
}).index('by_userId', ['userId']);
images: defineTable({
userId: v.id("users"),
...
}).index('by_userId', ['userId']);
now you can access all images associated with a user from the _id attribute of the user
const images = await ctx.db.query("images")
.withIndex("by_userId", q => q.eq("userId", <_id attribute of user>)
.collect();
const images = await ctx.db.query("images")
.withIndex("by_userId", q => q.eq("userId", <_id attribute of user>)
.collect();
you should look into these two articles
Bogdan
Bogdan16mo ago
Functional Relationships: Helpers
In this post, we’ll look at some helper functions to help write code to traverse relationships in a readable, predictable, and debuggable way.
Bogdan
Bogdan16mo ago
Relationship Structures: Let's Talk About Schemas
In this post we’ll look at some patterns for structuring relationships in the Convex database.

Did you find this page helpful?