Relational Schema
In this guide, you will explore the relational data schema on Back4App by saving and querying related data objects.
At Back4App, different classes can share the same object. Internally, the Parse framework will store the referred object in just one place to maintain consistency. That can give you extra power when building and running complex queries.
Back4App supports relational queries by using the data types Pointers and Relations. In this guide, you will see how to query data related using Relations.
- To store and query relational data objects on Back4App
To complete this tutorial, you will need:
An app created at Back4App.
Follow the Create a new App tutorial to learn how to create an app at Back4App.
Install Parse SDK.
Follow the Connect to Back4App tutorial to learn how to install Parse SDK to your application.
Let’s first create two sample classes of Parse.Object in your test app and populate it with some data, so that you can query them later. The class State contains the text field state_name. The class City has the text field city_name and a pointer field state, redirecting to the State class.
You don’t need to specify ahead of time what keys exist on each Parse.Object, the sample classes will be created with the corresponding types in your Back4App app automatically, including the relations between both classes.
Find below code samples for most of the Parse SDKs.
If you have a custom Parse Server URL, replace the default (parseapi.back4app.com) with yours.
Isn’t Parse SDK working properly? Make sure you have installed it correctly by checking the complete Install Parse SDK guide projects. Links below.
JavaScript - Android - PHP - REST API
The code sample won’t work as expected if you haven’t installed Parse SDK correctly for the technology of your project.
Now that you have created the classes and their respective objects, let’s perform two different relational queries. We will first look for cities that are localized in a certain state by using the Parse.Query.equalTo method. In this example, let’s search for all the cities that are in California State. The second relational query will retrieve every State object that is related to any City object, simulating an SQL JOIN query. For instance, we will retrieve every city that has a defined pointer to any State.
If you have a custom Parse Server URL, replace the default (parseapi.back4app.com) with yours.
Isn’t Parse SDK working properly? Make sure you have installed it correctly by checking the complete Install Parse SDK guide projects. Links below.
JavaScript - Android - PHP - REST API
Now you can keep exploring Cloud Functions on your preferred technology or explore and also learn how to use cloud functions to make integrations, unit tests, and more.
At this point, you have learned how to perform basic relational queries with Parse.Object using Parse SDK. If you want to perform more advanced queries you can go through our specific guides for each Parse SDK, such as the React Native JOIN queries guide and the React relational queries guide.