Noob - thing inside a thing?

Hi all - I’m trying to add a twist to the “Build a reddit-like app to vote on city names” tutorial. In the tutorial, you make a data type called City with two fields: Name and Votes. The user inputs a city name and clicks a button to trigger a workflow that adds the user’s city to the City data type. Then you display City in a repeating group, sorted by reddit-style voting.

Just as an experiment, I’m trying to complicate this idea a bit. In my version, the user inputs a country and several cities within that country. A repeating group then shows all previously submitted countries. Clicking on a country takes the user to a second page where all previously submitted cities in that country are displayed in a repeating group and sorted by votes as above.

Seems like I need a data type City just like before, but each row of Country would need its own version of City embedded in it.

Being a complete noob, I’m having a hard time in general, but especially with the data structure. Any suggestions would be appreciated.

Hi @qooqu, welcome!

In classical database terms, this would be a one-to-many relationship (one country can have mane cities, but a city can only belong to one country). In Bubble, the definition is a bit different, but the mechanics are the same.

  1. Create data tyoe country
    You already have a data type called City, and you can now add another one called Country, with a name field (and vote, if you want to vote for countries as well)

  2. Add Country to City
    Go into the City data type, and add a new field. In the list of field types, you pick Country. When this is added, you can add a Country to the City, meaning you have specificed which Country the City belongs to.

  3. Add Cities to Countries
    Now you need to repeat this action, but with a small twist. You do the vice versa of the above, and add City to the Country data type instead, but this time check the box This field is a list. By doing this, you can add not just one, but several cities. In other words, the country will include a list of the cities that belong to it.

When this is done, you can access the data both ways: you can look up a city and see which city it belongs to, and you can look up a country and see a list of cities that belong to it.

6 Likes

Thank you so much @petter!

I googled “one-to-many” which led me to databaseprimer.com. They talk about table keys which immediately made a lot of sense to me. I came back to my bubble app and found we have access to table keys via “unique id”.

So my solution is now:
A. User input fields: country, city1, city2, city3
B. Submit button triggers workflow:

  1. Create row in Country
  2. Create row in City with city1. On same row in City, include country via “result of step1’s unique id”
  3. same as step 2 for city2
  4. same as step 2 for city3

Then I pass Country to my voting page and use “current page Country’s unique id” to filter the cities.

Thanks again!

That’s right about the unique ID, although in Bubble you generally don’t need to reference it too often, as it’s mostly automated. But it makes sense to know a bit about what’s going on under the hood.

Glad I could help you out!

Agreed. If you are searching for unique ids in your searches then you need a very good reason to be doing so. Mostly, you don’t need to.

What about searching for a name? Even if first and last name are separate fields, if you get enough names eventually you’ll have two people named “John Smith.” It seems in this situation (or in a lot of other similar situations) you’d need to reference the unique ID. Another example would be cities that have the same name (like Springfield).

Or is there another simpler way that I don’t understand. Brand new to bubble, so maybe I’m just missing something obvious.

Hi there,

It really depends on your particular usage.

If you are searching for people, then pulling back two (or more) “John Smiths” is fine.

However in the case where this is something like …

A DEPARTMENT has a single manager, and this is an EMPLOYEE.

You are right, if you are storing “John Smith” as the name of the and there are two employees - absolutely there is an issue. Which John ?

You deal with in Bubble with setting the “Field Type” of the Manager field to EMPLOYEE. If you scroll down you will see a list of all the other tables you have already set up.

image

This means that Bubble does the link for you.

So now, in your app, when you are setting up the department and you need to add the manager, then you can do your search for name … up comes both Johns … you pick the right one, then you set the Manager field to be that record.

The link is made.

This means your app is now much simpler.

You can say show me this Department’s Manager’s First Name. It will navigate the relationship for you without needing to do a search.