Relational queries

Hey. I have 2 tables. Courses table has “Students” column which is in relation with “_Users” table. I want to get students who is related to certain Course. I tried multiple solutions I found in docs and youtube videos but none of them helped. How can i get students that related to that course?

Much love and thanks

Course table
Ekran görüntüsü 2022-06-24 223424
User table
Ekran görüntüsü 2022-06-24 223457

a simple query doesn’t work?
what did you try?

you can make multiple queries too

This is last i tried. I tried a lot of things i cant understand what im doing anymore actually. Stuck here for 2 days.

const user =  Moralis.Object.extend("_User");

   const Courses =  Moralis.Object.extend("Courses");

   const innerQuery = new Moralis.Query(user);


   const query = new Moralis.Query(Courses);

   query.matchesQuery("Students", innerQuery);

   const result = await query.find();

   console.log("result", result);

For users, you will need to use master key parameter to get data about any user as there are ACLS in every row in user class

1 Like

I tried it but didnt work. First result is without masterkey, second one is with masterkey.

Even though i change all the ACLs to public access. Do i still need masterkey for access users ?

how do you go from student to user?

the students are simple users? same thing?

Yes. Students are a column in Courses table. Relation to _Users table


And i add users to that relation so it shouldnt be empty

and what information you want to get in this case with a query? what would be the expected result?

I want to get all users in this relation. Which means all users in Students column

if you do a simple query in courses class, what do you get as result?

 async function getData() {
    const query = new Moralis.Query("Courses");
    const result = await query.find();
    console.log("result", result);

i can get list of course names but cant access Students column. This goes like infinite loop. Cant get students information


can you show more on how those students data looks like in that result?

this goes forever.

strange, in initial query, can you try something like this?

 async function getData() {
    query = new Moralis.Query("Courses");
    result = await query.find({useMasterKey: true});
    console.log("result", result);

I tried it before. Same result

this seems to work fine for roles:

Moralis.Cloud.define("test_relation", async (request) => {
    query = new Moralis.Query("_Role")
    result = await query.find({useMasterKey: true});
    q2 = result[0].get('users').query()
    r = await q2.find({useMasterKey: true})
    return r;
1 Like

I tried it and looks fine. I dont have enough knowledge on cloud functions so i will look at the docs.
Thank you for your help!

1 Like

the idea is that in cloud code you can use master key, other than that the syntax is the same as if you make the query directly in front end