Skip to content
Back

Two way relationship does not set id in one of the two documents.

  • 0
  • Databases
  • Cloud
Tomic R.
12 Jan, 2025, 09:36

Hi! I have a two-way relationship, 1-1, in appwrite. The two collections are "page" and "metadata". I have just created the new table (metadata table) and added the relationship, so currently, all the values for the relationship in the page table are null + there are no documents in the metadata table (obviously).

But I don't want that. I want that every page has a relationship to metadata. That's why I am currently creating a migration script, and one of the lines look like this:

TypeScript
 final metadataResponse = await _database.updateDocument(
          databaseId: AppEnv().accommodationDatabaseId,
          collectionId: AppEnv().pageCollectionId,
          documentId: pageId,
          data: {
            'metadata': {
              'visibleAtCheckIn': false,
              'visibleAtCheckOut': false,
              'titleImage': null,
            },
          },
        );

Essentially, I create the metadata through the page collection. This works kinda great. It creates the new metadata document, and the metadata attribute in the page table is filled with the id of the newly created metadata. But as already said, I have a two way relationship. The problem is that currently, the "page" attribute in the metadata table is still n/a, even though "metadata" in the page table is filled.

This also does not work:

TypeScript
final metadataResponse = await _database.createDocument(
  databaseId: AppEnv().accommodationDatabaseId,
  collectionId: AppEnv().pageMetadataCollectionId,
  documentId: ID.unique(),
  data: {
    'visibleAtCheckIn': false,
    'visibleAtCheckOut': false,
    'titleImage': null,
    'page': pageId,
  }

As I get this error: TypeError: Instance of 'JSArray<dynamic>': type 'List<dynamic>' is not a subtype of type 'Map<String, dynamic>'

So my question is: How can I create the metadata documents and have a bug-free relationship?

TL;DR
Developers created a migration script to establish a two-way relationship between the "page" and "metadata" collections in appwrite. The issue arises as the "page" attribute in the metadata table remains null even after the metadata attribute in the page table is filled. An error is encountered when attempting to create metadata documents. To resolve the issue and create metadata documents with a bug-free relationship, use the following updated approach: - Modify the metadata document creation snippet to ensure it correctly sets the relationship: ```dart final metadataResponse = await _database.createDocument( databaseId: AppEnv().accommodationDatabaseId, collectionId
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