N:N Relationship
11 min
many to many relationship on flutter introduction in the previous guide, we learned how to use one to many relations and we will continue with our project in this guide, we will focus on the most common relation many to many there are three ways to create a many to many relation in parse the first is using the parse \<font color="#2166ae">relations\</font> , which is the fastest in creation and query time we will use this in this guide the second is using \<font color="#2166ae">arrays\</font> of \<font color="#2166ae">pointers\</font> which can lead to slow query times depending on their size the third is using \<font color="#2166ae">jointable\</font> where the idea from classical database when there is a many to many relation, we combine every \<font color="#2166ae">objectid\</font> or \<font color="#2166ae">pointer\</font> from both sides together to build a new separate table in which the relationship is tracked in this guide you will implement a \<font color="#2166ae">many to many\</font> relationship on a flutter book registration app using the parse \<font color="#2166ae">relations\</font> you will learn how to create and query many to many data relations and how to perform queries returning data from related objects, using back4app and the flutter sdk prerequisites android studio https //developer android com/studio or vs code installed https //code visualstudio com/ (with plugins https //docs flutter dev/get started/editor dart and flutter) note the flutter app created in previous guide complete the previous guide so you can have a better understanding of the \<font color="#2166ae">one to may relationship\</font> class a device (or virtual device) running android or ios 1 run the book app template if you have not completed the previous guide you can clone and run the complete book flutter app https //github com/templates back4app/flutter associations project from our repository you can also take a look at the previous guide https //www back4app com/docs/flutter/parse sdk/data objects/%7bsite baseurl%7d%7d//flutter/parse sdk/data objects/flutter one to many relationship to better understand the app template below you can find a visual representation of book registration data model 2 save a book object and its author’s open the flutter project from the previous guide one to many relationship on flutter https //www back4app com/docs//flutter/parse sdk/data objects/flutter one to many relationship search for the function \<font color="#2166ae">dosavebook\</font> in file \<font color="#2166ae">main dart\</font> , and replace with the code below inside the \<font color="#2166ae">future\<void\> dosavebook()\</font> function below this function will create a new book in back4app data store with relations 1 final book = parseobject('book') 2 set('title', controllertitle text trim()) 3 set('year', int parse(controlleryear text trim())) 4 //the objectid will be converted to a pointer on the save() method 5 set('genre', parseobject('genre') objectid = genre objectid) 6 //you can also convert to a pointer object before the saving using the topointer() method 7 set('publisher', 8 (parseobject('publisher') objectid = publisher objectid) topointer()) 9 //saving a list of authors for the book 10 addrelation( 11 'authors', 12 authors 13 map((o) => parseobject('author') objectid = o objectid) 14 tolist()); 15 16 await book save(); to build this function, follow these steps 1\ create a new instance of the parse \<font color="#2166ae">book\</font> class with the command \<font color="#2166ae">parseobject('book')\</font> 2 use the \<font color="#2166ae">set\</font> function to set the fields for this object 2 1 \<font color="#2166ae">title\</font> is a text attributes that receive value from the text controller 2 2 \<font color="#2166ae">genre\</font> receives the value by defining a \<font color="#2166ae">parseobject\</font> with the \<font color="#2166ae">objectid\</font> of the selected \<font color="#2166ae">genre\</font> ( parse will convert to pointer on save ) 2 3 \<font color="#2166ae">publisher\</font> receives the value by defining a \<font color="#2166ae">parseobject\</font> with the \<font color="#2166ae">objectid\</font> of the selected \<font color="#2166ae">publisher\</font> ( note that we can specify for parse that we want to save as a \<font color="#2166ae">pointer\</font> using the \<font color="#2166ae">topointer()\</font> method ) 2 4 \<font color="#2166ae">authors\</font> we call the \<font color="#2166ae">addrelation\</font> method of \<font color="#2166ae">parseobject\</font> , sending a list of \<font color="#2166ae">parseobject\</font> with the \<font color="#2166ae">objectid\</font> of the selected \<font color="#2166ae">authors\</font> 3 call the \<font color="#2166ae">save\</font> function in \<font color="#2166ae">parseobject\</font> , which will effectively register the object to your database in the back4app dashboard run the app and test the new \<font color="#2166ae">dosavebook()\</font> function first, access the dashboard and delete the books that were previously registered in the previous guide click on the \<font color="#2166ae">add book\</font> button fill book information with authors click on \<font color="#2166ae">save book\</font> button to confirm that the new object is save in the database with relations, you can access the \<font color="#2166ae">back4app dashboard\</font> and access \<font color="#2166ae">book\</font> class clicking on the object pointer/relation value in your dashboard will take you to the referenced object entry it may seem like a harmless feature, but this makes debugging and error tracing much quicker than searching for it manually 3 query the book details with relations this function will query book details in back4app database, returning relationship data in some situations, you want to return multiple types of related objects in one query you can do this with the \<font color="#2166ae">includeobject\</font> method in our example, we want to return the books, with information from genre and publishers search for the function \<font color="#2166ae">getbookdetail\</font> in the file \<font color="#2166ae">main dart\</font> , then replace the code below inside \<font color="#2166ae">getbookdetail(parseobject book)\</font> function 1 querybuilder\<parseobject> querybook = 2 querybuilder\<parseobject>(parseobject('book')) 3 whereequalto('objectid', book objectid) 4 includeobject(\['publisher', 'genre']); 5 6 final parseresponse responsebook = await querybook query(); 7 8 if (responsebook success && responsebook results != null) { 9 final book = (responsebook results first) as parseobject; 10 booktitle = book get\<string>('title'); 11 bookyear = book get\<int>('year'); 12 bookgenre = book get\<parseobject>('genre') get\<string>('name'); 13 bookpublisher = book get\<parseobject>('publisher') get\<string>('name'); 14 loadeddata = true; 15 } 16 17 querybuilder\<parseobject> queryauthors = 18 querybuilder\<parseobject>(parseobject('author')) 19 whererelatedto('authors', 'book', book objectid); 20 21 final parseresponse responseauthors = await queryauthors query(); 22 23 if (responseauthors success && responseauthors results != null) { 24 bookauthors = responseauthors results 25 map((e) => (e as parseobject) get\<string>('name')) 26 tolist(); 27 } to build this function, follow these steps create an instance of \<font color="#2166ae">parsequery\</font> object for \<font color="#2166ae">book\</font> class insert a condition in the query, to search \<font color="#2166ae">books\</font> where \<font color="#2166ae">objectid\</font> field is equal \<font color="#2166ae">objectid\</font> of the selected book we use the includeobject method, informing the fields of the pointers that we want to return the data in the same query genre and publisher you can also do multi level includeobject using dot notation exemple ` includeobject(\[‘post’, ‘post authors’]); do a query’s search method using \<font color="#2166ae">query()\</font> method if the operations succeed, object in \<font color="#2166ae">book\</font> will be returned we use the get method to retrieve the data for fields that are pointers, we will first need to retrieve the pointer, then obtain its data example \<font color="#2166ae">bookgenre = book get\<parseobject\>('genre') get\<string\>('name');\</font> in the second stage of processing, we need to recover the authors associated with the book to build this function, follow these steps create an instance of \<font color="#2166ae">parsequery\</font> object for \<font color="#2166ae">authors\</font> class insert a condition in the query, using \<font color="#2166ae">whererelatedto\</font> operator to search \<font color="#2166ae">authors\</font> relationship with \<font color="#2166ae">book\</font> , where \<font color="#2166ae">book\</font> is equal \<font color="#2166ae">objectid\</font> of the selected book do a query’s search method using \<font color="#2166ae">query()\</font> method if the operations succeed, object in \<font color="#2166ae">book\</font> will be returned we use the get method to retrieve the data run the app and test the new query first, click on the \<font color="#2166ae">list publisher/book\</font> button select a book from the list the next screen will display the data for the books and their relationships it’s done! at this point, you learned how to create and query many to many relations and how to perform queries returning data from related objects in parse on \<font color="#2166ae">flutter\</font>