Bubble has given some information on this,plz go through.
Connecting types
âHow should I structure my databaseâ is a common - and important! - question. Luckily, your Bubble database is quite flexible and there are many right answers. Itâs also easy to âfixâ later on if you realized you made a wrong turn. This section walks through a few common patterns for connecting one data type with another, especially in cases where one Thing might relate to many Things of another data type.
Technical users might recognize some of this content as explaining how "foreign key" or "many-to-many relationships" work in Bubble. Bubble does not use these terms and these concepts don't exist in quite the same way in a Bubble database, but you can still easily achieve the same end results.
Use cases
When you define a new field on a data type in your appâs Data tab, you can specify whether itâs a text, number, yes/no, or even another data type. You can also specify whether that field should just have one value of that type, or a list of values of that type.
Examples of when a field is a (singular) value of another data type:
-
A BlogPost data type has a Creator field which is a User
-
An Event data type has a Venue field which is a Building
-
A JobCandidate data type has a Degree From field which is a School
-
A Community data type has a State field which is a State
Examples of when a field is a list of values of another data type:
-
A BlogPost data type has a field for Tag which is a list of Tags
-
A Project data type has a field for Follower which is a list of Followers
-
A User of your marketplace app has a field for Favorite which is a list of Properties that they starred
-
A Contractor in your dispatching app has a field for Job which is a list of Jobs, past and present
For those coming from a technical database background, in Bubble you don't have to declare whether a relationship is "one-to-many" or "many-to-many". If you have data type A with thing a and data type B with thing b, and A has a field that's of type B, Bubble doesn't care how many a's are connected with a given b. You just have to tell Bubble that the field is type B, and whether it's a single or list of values.
Example scenarios
Letâs run with the example of a Bubble app thatâs a blog, with the Post and Tag data types. An individual Post can have many Tags, and of course a Tag can be used on many Posts. How do you set this up?
The answer is âit dependsâ - like other areas of Bubble, you have the flexibility to build this in different ways, but there are tradeoffs that weâll cover here.
Here are the different major options you could use to create this relationship:
Option 1: Each Post has a Tag field thatâs a list of Tags
This means that âBlog Post #123â might have Tags âCookingâ, âTravelâ and âFunâ, all stored on the Tag field of Post.
The pro with this approach is that when you have a Post and want to look up which Tags it has, thatâs easy! Letâs say you have a page with assigned data type of Post (each Post on its own page, for example) - to get the Tags would be Current Page's Tags
. This is a quick query.
The con with this approach is that if you have a Tag and want to look up which Posts have that Tag, the query is a bit more roundabout. Letâs say each Tag also has its own page that lists all the Posts with that Tag. To find that list of Posts, youâd have to Do a search for
Posts with a filter for Tag contains Current Page's Tag
. This is a slower query (for most normal-sized blogs, this is probably not noticeably bad from a performance standpoint, but you can imagine parallel situations that have much bigger scale).
Option 2: Each Tag carries a list of Posts that have that Tag
This means that Tag âCookingâ has a field with âBlog Post #123â, âBlog Post #153â, âBlog Post #89â, and so on.
The pros and cons here are the inverse of Option 1 above. The query to get all Posts of a particular Tag is easy, but to find all Tags on a single Post is a slightly longer query.
Option 1+2: You could do both of the above!
Each Post keeps track of which Tags it has, and each Tag keeps track of which Posts have that Tag.
Thereâs nothing stopping you from doing both of the above options! This way itâs quick to get all the Tags on a given Post and also quick to get all the Posts of a given Tag.
The downside here is that you need to do double the work. Each time a Post gets assigned a new Tag, you need to remember to add it to the Postâs list of Tags and also the Tagâs list of Posts. Not too much extra work, but it can be easy to forget to do this, leading to data consistency problems.
Option 3: A new PostTag data type that has one field for Post and one for Tag
This means that you create a new PostTag thing to handle each Post + Tag pairing. So one PostTag for âBlog Post #123â + âCookingâ, another PostTag for âBlog Post #123â + âTravelâ, and another PostTag for âBlog Post #123â + âFunâ.
(Technical folks may recognize this as a âjoining tableâ.)
This is kind of the âcompromiseâ solution between Options 1 and 2. To find all the Tags of a Post, youâd have to Do a search for
all PostTags where Post = Current Pageâs Post, or the opposite to find all the Posts of a Tag. This makes the search in both directions a bit slower than the fast search offered by Option 1 or 2. Also, note another small disadvantage is that itâs possible to create duplicate PostTag entries, whereas if you were using a list in Option 1 or 2, Bubble automatically de-dupes entries in a list.
Why would somebody choose this option then? The answer is that itâs much more scalable. If a data type has a list of things for a given field, that list maxes out at 10,000 entries. This may seem like a lot, but imagine, for example, how many Pinterest posts one User might heart over their lifetime! Option 1 or 2 would break at that scale, but Option 3 would handle it fine.
In practice, if youâre creating this kind of relationship and expect one thing to have a list of more than 100 values of another thing, we recommend going with Option 3 .
To summarize, the option you choose will depend first on the scale you need for your Bubble app (which might determine if Option 1 or 2 is even an option) and second on which direction(s) you might want to make queries super fast. Note that you could also start with Option 1 or 2 then later migrate to Option 3).
Special example: when friendships are complicated
Many apps might want to build a âmutualâ relationship, like a User being able to friend another User. The same options generally apply, but things just get a little confusing because everything is done with the same data type! In this case, you could go with Option 1 or 2, but if Users Alice and Bob are friends, it will get confusing if Bob is in Aliceâs list of Friends and not vice versa. Option 1+2 is a better way to go. For Option 3, you may want to modify the setup a little bit - instead of one field for one User and a second field for the other, instead you may want to have one field for âFriendship Membersâ thatâs a list of Users - this puts Alice and Bob âon the same footingâ so to speak, so you donât have to worry about whoâs âFriend1â vs âFriend2â. (Since social networks tend to have a lot of such relationships, we recommend trying Option 3 for this case.)