Back

[SOLVED] Need help with relationships

  • 0
  • Databases
  • Web
RangerDev πŸ”»πŸ”»πŸ”»πŸ”»
27 Dec, 2023, 20:37

Hello! I am trying to make a relation ship with students, parents, and class. Students can contain 1 parent while parents can contain many students. Same with classes. However, when trying to link those relationships using document IDs it does not work. Please help....

Here is the code:

TypeScript
    import { appwriteDatabases,appwriteUser } from "$lib/appwrite";
    import { DB_ID,COLLECTION } from "$lib/ids";
    import { Query,ID } from "appwrite";

    let uuid = '';
    let parentDBID = '';
    let children = [] as any[];

    let newChildName = '';
    let newChildClass = '';

    async function addChild() {
        console.log(newChildName);
        console.log(newChildClass);
        console.log(parentDBID);
        await appwriteDatabases.createDocument(DB_ID,COLLECTION.Students,ID.unique(),
            {
                Name:newChildName,
                // remove the space at the end
                class:[newChildClass],
                parents:[parentDBID]
            }
        ).then((res) => {
            console.log(res);
        }).catch((err) => {
            console.log(err);
        });
    }

    async function getChildren() {
        appwriteUser.get().then((res) => {
            uuid = res['$id'];
            console.log('UUID '+uuid);
            appwriteDatabases.listDocuments(DB_ID,COLLECTION.Parents,[Query.equal('uid',[uuid])]).then((res) => {
                parentDBID = res.documents[0]['$id'];
                children = res.documents[0]['students'];
                console.log(children);
        }).catch((err) => {
            console.log(err);
        });
        }).catch((err) => {
            console.log(err);
        });
    }

    getChildren();

NOTE: The variable newChildClass is a DB id... please help!

TL;DR
The user is encountering issues with creating relationships between students, parents, and classes in their code. They are trying to link these relationships using document IDs, but it is not working. They have shared their code and are requesting assistance. Solution: In the code, the user is passing an array for the parent relationship, even though a student can only have one parent. The solution is to pass a single parent ID instead of an array. Additionally, there is a space at the end of the "class" field value that should be removed.
RangerDev πŸ”»πŸ”»πŸ”»πŸ”»
27 Dec, 2023, 20:38

On the console it shows n/a. There are 0 errors....

RangerDev πŸ”»πŸ”»πŸ”»πŸ”»
27 Dec, 2023, 21:29

@Steven can u help please?

Drake
27 Dec, 2023, 21:45

Please don't tag people as it can be very disruptive. Just post and wait

Drake
27 Dec, 2023, 22:43

what's it say if you look at the JSON for the document?

RangerDev πŸ”»πŸ”»πŸ”»πŸ”»
27 Dec, 2023, 23:38

It does not show

RangerDev πŸ”»πŸ”»πŸ”»πŸ”»
27 Dec, 2023, 23:40

this is it pretty much

RangerDev πŸ”»πŸ”»πŸ”»πŸ”»
27 Dec, 2023, 23:40

nothing about the relations

Drake
28 Dec, 2023, 00:37

what are the permissions on the related collections?

RangerDev πŸ”»πŸ”»πŸ”»πŸ”»
28 Dec, 2023, 00:52
RangerDev πŸ”»πŸ”»πŸ”»πŸ”»
28 Dec, 2023, 00:52
RangerDev πŸ”»πŸ”»πŸ”»πŸ”»
28 Dec, 2023, 00:52
RangerDev πŸ”»πŸ”»πŸ”»πŸ”»
28 Dec, 2023, 00:52

its the same for all 3 collections

Drake
28 Dec, 2023, 01:02

If a student can only have 1 parent, why are you passing an array?

RangerDev πŸ”»πŸ”»πŸ”»πŸ”»
28 Dec, 2023, 01:04

oh...

RangerDev πŸ”»πŸ”»πŸ”»πŸ”»
28 Dec, 2023, 01:04

I thought the square brackets were required....

RangerDev πŸ”»πŸ”»πŸ”»πŸ”»
28 Dec, 2023, 01:07

it works tyms!

RangerDev πŸ”»πŸ”»πŸ”»πŸ”»
28 Dec, 2023, 01:08

[SOLVED] Need help with relationships

Reply

Reply to this thread by joining our Discord

Reply on Discord

Need support?

Join our Discord

Get community support by joining our Discord server.

Join Discord

Get premium support

Join Appwrite Pro and get email support from our team.

Learn more