From c3484d686c32ce0b4bc5f2b35954b633edb0cd47 Mon Sep 17 00:00:00 2001 From: Michael Macaulay <56690114+MichaelMacaulay@users.noreply.github.com> Date: Thu, 30 Jan 2025 15:38:56 -0500 Subject: [PATCH] Adding example --- .../en/subgraphs/developing/creating/ql-schema.mdx | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/website/pages/en/subgraphs/developing/creating/ql-schema.mdx b/website/pages/en/subgraphs/developing/creating/ql-schema.mdx index d148cf2ab1fb..27562f970620 100644 --- a/website/pages/en/subgraphs/developing/creating/ql-schema.mdx +++ b/website/pages/en/subgraphs/developing/creating/ql-schema.mdx @@ -160,6 +160,18 @@ type TokenBalance @entity { } ``` +Here is an example of how to write a mapping for a subgraph with reverse lookups: + +```typescript +let token = new Token(event.address) // Create Token +token.save() // tokenBalances is derived automatically + +let tokenBalance = new TokenBalance(event.address) +tokenBalance.amount = BigInt.fromI32(0) +tokenBalance.token = token.id // Reference stored here +tokenBalance.save() +``` + #### Many-To-Many Relationships For many-to-many relationships, such as users that each may belong to any number of organizations, the most straightforward, but generally not the most performant, way to model the relationship is as an array in each of the two entities involved. If the relationship is symmetric, only one side of the relationship needs to be stored and the other side can be derived.