Skip to content
Init is coming / May 19 - 23
Back

Query.select() on relationships

  • 1
  • Databases
  • Web
  • Cloud
Varun
26 Oct, 2024, 17:11

Normal Output:

TypeScript
"questions": {
      "total": 2,
      "documents": [
        {
          "order": 1,
          "$id": "quiz_question_1",
          "questionId": {
            "$id": "question_1",
            "$databaseId": "...",
            "$collectionId": "..."
          }
        },
        {
          "order": 2,
          "$id": "quiz_question_2",
          "questionId": {
            "$id": "question_2",
            "$databaseId": "...",
            "$collectionId": "..."
          }
        }
      ]
    }

but if i do

TypeScript
Query.select(["question", "order"]);

it throws an error

TypeScript
AppwriteException: Cannot select attributes: questionId
TypeScript
code: 400,
type: 'general_query_invalid',
response: {
  message: 'Cannot select attributes: questionId',
  code: 400,
  type: 'general_query_invalid',
  version: '1.6.0'
}
TL;DR
Developers are encountering an issue with `Query.select()` not handling nested attributes like `questionId` directly. The suggested workaround is to fetch the entire document and filter out the necessary fields in the application logic. Attempting to select nested attributes directly leads to an error.
Varun
26 Oct, 2024, 17:12

sorry for typo: the query was

TypeScript
Query.select(["questionId", "order"]);
Guri
26 Oct, 2024, 21:03

The issue seems to arise from attempting to select nested attributes, such as questionId

Guri
26 Oct, 2024, 21:04

Since Query.select isn't able to handle nested attributes directly,

Guri
26 Oct, 2024, 21:04

You can work around this by fetching the entire document and then extracting the specific fields you need in your application logic.

Guri
26 Oct, 2024, 21:06
TypeScript
const response = await database.listDocuments("<DATABASE_ID>", "<COLLECTION_ID>");
const documents = response.documents.map(doc => ({
  order: doc.order,
  questionId: doc.questionId
}));

console.log(documents);
Guri
26 Oct, 2024, 21:07

This way, you get all the necessary data and then filter out the required attributes.

Varun
28 Oct, 2024, 07:22

thank you @Guri

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