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.