#Lesson 2 - References
This section covers adding reference fields to the models built in lesson 1.2.
References connect models to each other. The connections you configure here are what make it possible to query a product and receive its categories in a single API call.
Without references, a product entry has no connection to its categories, and a landing page has no connection to its blog posts. This is why references are a critical part of the schema.
By the end of lesson 2.1, the following model relationships are configured:
- Products connected to Product categories (many-to-many, two-way)
- Landing page connected to Blog posts, Product categories, and Seller information (one-way)
#How references work
The way you configure reference fields determines the following:
Type controls which models can be referenced. Selecting a single model means only that model's entries can be connected. Selecting multiple models creates a union reference.
Direction controls which side of the relationship can be queried. A one-way reference allows querying only from the model where the field lives. A two-way reference adds a reverse field to the referenced model, so you can query from either side.
Cardinality controls how many entries can be connected. The combination of direction and cardinality produces four reference patterns:
| Pattern | What it means | When to use it |
|---|---|---|
| One-to-one | One entry connects to one entry | A landing page linked to one seller information entry |
| One-to-many | One entry connects to many entries | A landing page linked to multiple blog posts |
| Many-to-many | Many entries connect to many entries | Products assigned to multiple categories |
| Many-to-one | Many entries connect to one entry | Multiple landing pages linked to one seller information entry |
The references created in this lesson impact the queries in lesson 7.
For the complete reference field documentation, see References.
#Lessons in this section
| Lesson | What you will do |
|---|---|
| 2.1 Configure reference fields | Add reference fields to the Product and Landing page models and configure their type, direction, and cardinality |
#What's next
Lesson 2.1 - Configure reference fieldsOr, go to the Tutorial overview for the full lesson list.