Get started
Relational Schema
14 min
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 a back4app application create an app guide https //www back4app com/docs/get started/new parse app parse sdk installed installation guide https //www back4app com/docs/get started/parse sdk 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 async function createstateandcity() { try { // create a state const state = parse object extend('state'); const california = new state(); california set('state name', 'california'); const savedstate = await california save(); console log(`state created with objectid ${savedstate id}`); // create a city with a pointer to state const city = parse object extend('city'); const losangeles = new city(); losangeles set('city name', 'los angeles'); losangeles set('state', savedstate); const savedcity = await losangeles save(); console log(`city created with objectid ${savedcity id}`); } catch (error) { console error('error creating state and city ', error message); } } createstateandcity(); flutter future\<void> createclasses() async { await parse() initialize('your app id', 'https //parseapi back4app com/', clientkey 'your client key', autosendsessionid true); // create a state final state = parseobject('state') set('state name', 'california'); final stateresult = await state save(); if (stateresult success) { // create a city with a pointer to state final city = parseobject('city') set('city name', 'los angeles') set('state', state); final cityresult = await city save(); if (cityresult success) { print('state and city created successfully '); } } } android public class mainactivity extends appcompatactivity { @override protected void oncreate(bundle savedinstancestate) { super oncreate(savedinstancestate); setcontentview(r layout activity main); // initialize parse parse initialize(new parse configuration builder(this) applicationid("your app id") clientkey("your client key") server("https //parseapi back4app com/") build() ); // create state and city createclasses(); } private void createclasses() { // create a state parseobject state = new parseobject("state"); state put("state name", "california"); state saveinbackground(e > { if (e == null) { log d("parse", "state created successfully with objectid " + state getobjectid()); // save city with a pointer to state parseobject city = new parseobject("city"); city put("city name", "los angeles"); city put("state", state); city saveinbackground(ex > { if (ex == null) { log d("parse", "city created successfully with objectid " + city getobjectid()); } else { log e("parse", "failed to create city " + ex getmessage()); } }); } else { log e("parse", "failed to create state " + e getmessage()); } }); } } ios struct state parseobject { var objectid string? var state name string? var createdat date? var updatedat date? } struct city parseobject { var objectid string? var city name string? var state state? var createdat date? var updatedat date? } func createclasses() async throws { try await parseswift initialize(applicationid "your app id", clientkey "your client key", serverurl url(string "https //parseapi back4app com")!) let state = state(state name "california") let savedstate = try await state save() var city = city(city name "los angeles", state savedstate) city = try await city save() print("state and city created successfully ") } php require 'vendor/autoload php'; use parse\parseclient; use parse\parseobject; parseclient initialize('your app id', 'your client key', 'https //parseapi back4app com/'); try { // create a state $state = new parseobject("state"); $state >set("state name", "california"); $state >save(); // create a city with a pointer to state $city = new parseobject("city"); $city >set("city name", "los angeles"); $city >set("state", $state); $city >save(); echo "state and city created successfully "; } catch (exception $ex) { echo "error " $ex >getmessage(); } net namespace parseexampleapp { class program { static async task main(string\[] args) { // initialize parse parseclient initialize(new parseclient configuration { applicationid = "your app id", server = "https //parseapi back4app com/", key = "your client key" }); // call the method to create state and city await createclassesasync(); } static async task createclassesasync() { try { // create a state var state = new parseobject("state"); state\["state name"] = "california"; await state saveasync(); console writeline($"state created with objectid {state objectid}"); // create a city with a pointer to the state var city = new parseobject("city"); city\["city name"] = "los angeles"; city\["state"] = state; // set the pointer to the state await city saveasync(); console writeline($"city created with objectid {city objectid}"); } catch (exception ex) { console writeline($"error {ex message}"); } } } } rest api # create state curl x post \\ h "x parse application id your app id" \\ h "x parse rest api key your rest api key" \\ h "content type application/json" \\ d '{"state name" "california"}' \\ https //parseapi back4app com/classes/state \# use the objectid of the created state to create a city curl x post \\ h "x parse application id your app id" \\ h "x parse rest api key your rest api key" \\ h "content type application/json" \\ d '{"city name" "los angeles", "state" {" type" "pointer", "classname" "state", "objectid" "state object id"}}' \\ https //parseapi back4app com/classes/city 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 const statequery = new parse query("state"); statequery equalto("state name", "california"); statequery first() then(state => { const cityquery = new parse query("city"); cityquery equalto("state", state); return cityquery find(); }) then(cities => { cities foreach(city => { console log(`city ${city get("city name")}`); }); }) catch(error => { console error("error fetching cities ", error message); }); flutter final statequery = querybuilder(parseobject('state')) whereequalto('state name', 'california'); final stateresult = await statequery query(); if (stateresult success && stateresult results != null) { final state = stateresult results! first; final cityquery = querybuilder(parseobject('city')) whereequalto('state', state); final cityresult = await cityquery query(); if (cityresult success && cityresult results != null) { for (var city in cityresult results!) { print('city ${city get\<string>('city name')}'); } } } android parsequery\<parseobject> statequery = parsequery getquery("state"); statequery whereequalto("state name", "california"); statequery getfirstinbackground((state, e) > { if (e == null) { parsequery\<parseobject> cityquery = parsequery getquery("city"); cityquery whereequalto("state", state); cityquery findinbackground((cities, ex) > { if (ex == null) { for (parseobject city cities) { log d("parse", "city " + city getstring("city name")); } } else { log e("parse", "error fetching cities " + ex getmessage()); } }); } else { log e("parse", "error fetching state " + e getmessage()); } }); ios let statequery = state query("state name" == "california") statequery first { result in switch result { case success(let state) let cityquery = city query("state" == state) cityquery find { cityresult in switch cityresult { case success(let cities) cities foreach { city in print("city \\(city city name ?? "unknown")") } case failure(let error) print("error fetching cities \\(error localizeddescription)") } } case failure(let error) print("error fetching state \\(error localizeddescription)") } } php use parse\parsequery; // query state $statequery = new parsequery("state"); $statequery >equalto("state name", "california"); $state = $statequery >first(); if ($state) { // query cities $cityquery = new parsequery("city"); $cityquery >equalto("state", $state); $cities = $cityquery >find(); foreach ($cities as $city) { echo "city " $city >get("city name") "\n"; } } net var statequery = new parsequery\<parseobject>("state") whereequalto("state name", "california"); var state = await statequery firstasync(); if (state != null) { var cityquery = new parsequery\<parseobject>("city") whereequalto("state", state); var cities = await cityquery findasync(); foreach (var city in cities) { console writeline($"city {city\["city name"]}"); } } rest api # query state curl x get \\ h "x parse application id your app id" \\ h "x parse rest api key your rest api key" \\ "https //parseapi back4app com/classes/state?where={\\"state name\\" \\"california\\"}" \# query cities \# replace state object id with the objectid from the state query curl x get \\ h "x parse application id your app id" \\ h "x parse rest api key your rest api key" \\ "https //parseapi back4app com/classes/city?where={\\"state\\" {\\" type\\" \\"pointer\\",\\"classname\\" \\"state\\",\\"objectid\\" \\"state object id\\"}}" example 2 query states with related cities create a query that returns states connected to any city javascript const statequery = new parse query("state"); const cityquery = new parse query("city"); cityquery matchesquery("state", statequery); cityquery include("state"); cityquery find() then(cities => { cities foreach(city => { const state = city get("state"); console log(`city ${city get("city name")} belongs to state ${state get("state name")}`); }); }) catch(error => { console error("error fetching data ", error message); }); flutter final statequery = querybuilder(parseobject('state')); final cityquery = querybuilder(parseobject('city')) wherematchesquery('state', statequery) includeobject(\['state']); final cityresult = await cityquery query(); if (cityresult success && cityresult results != null) { for (var city in cityresult results!) { final state = city get\<parseobject>('state'); print('city ${city get\<string>('city name')} belongs to state ${state? get\<string>('state name')}'); } } android parsequery\<parseobject> statequery = parsequery getquery("state"); parsequery\<parseobject> cityquery = parsequery getquery("city"); cityquery wherematchesquery("state", statequery); cityquery include("state"); cityquery findinbackground((cities, e) > { if (e == null) { for (parseobject city cities) { parseobject state = city getparseobject("state"); log d("parse", "city " + city getstring("city name") + " belongs to state " + state getstring("state name")); } } else { log e("parse", "error " + e getmessage()); } }); ios let statequery = state query() let cityquery = city query(matchesquery(key "state", query statequery)) cityquery include("state") cityquery find { result in switch result { case success(let cities) cities foreach { city in if let state = city state { print("city \\(city city name ?? "unknown") belongs to state \\(state state name ?? "unknown")") } } case failure(let error) print("error \\(error localizeddescription)") } } php use parse\parsequery; $statequery = new parsequery("state"); $cityquery = new parsequery("city"); $cityquery >matchesquery("state", $statequery); $cityquery >includekey("state"); $cities = $cityquery >find(); foreach ($cities as $city) { $state = $city >get("state"); echo "city " $city >get("city name") " belongs to state " $state >get("state name") "\n"; } net var statequery = new parsequery\<parseobject>("state"); var cityquery = new parsequery\<parseobject>("city") wherematchesquery("state", statequery) include("state"); var cities = await cityquery findasync(); foreach (var city in cities) { var state = city get\<parseobject>("state"); console writeline($"city {city\["city name"]} belongs to state {state\["state name"]}"); } rest api curl x get \\ h "x parse application id your app id" \\ h "x parse rest api key your rest api key" \\ "https //parseapi back4app com/classes/city?where={\\"state\\" {\\"$inquery\\" {\\"classname\\" \\"state\\"}}}\&include=state" 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 https //docs parseplatform org to dive even deeper!