Back

Query on relationships

  • 1
  • Databases
Valou
14 Nov, 2024, 14:29

Hi, I am trying to select get info from a relationship. When I just list documents i get this output:

TypeScript
{
    "total": 1,
    "documents": [
        {
            "bib": 1,
            "name": "valou",
            "$id": "6735dffb0020d0a5fbb3",
            "$createdAt": "2024-11-14T11:33:16.056+00:00",
            "$updatedAt": "2024-11-14T11:33:16.056+00:00",
            "$permissions": [],
            "times": [
                {
                    "time": "2024-11-14T14:21:23.345+00:00",
                    "$id": "6735f95f000be8949b18",
                    "$createdAt": "2024-11-14T13:21:35.942+00:00",
                    "$updatedAt": "2024-11-14T13:21:35.942+00:00",
                    "$permissions": [],
                    "location": {
                        "name": "location_name",
                        "tns_name": "tns2",
                        "$id": "6735f9260034476bfcb7",
                        "$createdAt": "2024-11-14T13:20:39.468+00:00",
                        "$updatedAt": "2024-11-14T13:20:39.468+00:00",
                        "$permissions": [],
                        "$databaseId": "672fbf550001be0825cf",
                        "$collectionId": "6735df73000d17f21013"
                    },
                    "$databaseId": "672fbf550001be0825cf",
                    "$collectionId": "6735e0210021135b8061"
                }
            ],
            "$databaseId": "672fbf550001be0825cf",
            "$collectionId": "6735dfcb00216f8f8aac"
        }
    ]
}

However I want to filter out lots of the data and only show the name, bib and the time within the times object. How do i do this?

I have tried: Query.select(["bib", "name", "times"]) and Query.select(["bib", "name", "times.time"])

TL;DR
Developers are advised to manually create relationships when fetching data rather than relying on relationship attributes. Although queries are not officially supported on relationships and are currently in beta, a workaround is suggested to fetch parent and child items separately. To filter and display specific data from a relationship like name, bib, and time, developers should use `Query.select(["name", "bib", "times.time"])` instead of `Query.select(["bib", "name", "times"])` which will still display unnecessary data.
Kenny
14 Nov, 2024, 14:42

Relationships are in beta and queries are not officially supported on them.

Valou
14 Nov, 2024, 14:43

Too bad but thanks for the quick answer

Valou
14 Nov, 2024, 14:44

Is there perhaps an easy workaround that still allows me to run queries in the backend

Kenny
14 Nov, 2024, 14:55

I think right now, at this point the best thing to do is to manually create the relationships yourself when fetching the data. So instead of using the relationship attribute setup a foreign key attribute, then fetch the parent item, once you've got that fetch the child.

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