Get started

Relational Schema

14min

This guide explains how to work with relational schemas on Back4app, including creating related classes and performing efficient queries using Parse Server. You will learn how to use Pointers and Relations effectively, along with practical examples.

What is a Relational Schema?

A relational schema organizes data into different classes connected to each other. In Parse Server, these relationships are managed through:

  • Pointers: Refer directly to a single object.
  • Relations: Manage multiple connections (many-to-many relationships).

These tools allow you to perform complex queries efficiently and consistently.

Goals

By the end of this guide, you will be able to:

  • Create relationships between classes using Pointers and Relations.
  • Perform relational queries to retrieve connected data.
  • Optimize your schema for better performance.

Prerequisites

1 - Creating Related Classes

Practical Example: States and Cities

Imagine you want to model a system where cities are associated with states:

  • Class State with the field state_name.
  • Class City with the field city_name and a Pointer to State.

Creating Classes and Relationships

JavaScript
Flutter
Android
iOS
PHP
.NET
REST API


2 - Querying Related Data

Now that the data is related, you can perform queries to retrieve it.

Example 1: Fetch Cities in a Specific State

JavaScript
Flutter
Android
iOS
PHP
.NET
REST API


Example 2: Query States with Related Cities

Create a query that returns states connected to any city:

JavaScript
Flutter
Android
iOS
PHP
.NET
REST API


Best Practices

To effectively work with relational schemas using Parse Server in Back4App, follow these best practices to ensure performance, maintainability, and scalability:

Choose the Right Relationship Type

  • Use Pointers for one-to-one relationships, such as linking a user to their profile.
  • Use Relations for many-to-many relationships, such as linking a project to multiple tasks.

Efficient Querying

  • Use .include() to load related data in the same query, reducing the need for multiple requests.
  • Limit results using limit() and skip() to avoid fetching large datasets at once.
  • Index frequently queried fields to speed up searches.

Avoid Excessive Nesting

  • Keep queries flat to reduce complexity and improve performance. Use nested queries sparingly and only when necessary.

Offload Complex Queries to Cloud Code

  • For complex queries involving multiple relations or large datasets, offload these to Cloud Code to keep the client lightweight and responsive.

Conclusion

In this guide, you learned how to create relationships between classes and query-related objects on Back4app. Continue exploring the specific SDK documentation to dive even deeper!