Creating objects and fields with Schema Builder
Now, let's create some extra fields and relationships for our BIM DB app through Schema Builder:
- Go to Setup, search for Schema Builder and open it.
- If some objects are already preselected, just click the Clear All button under the Objects tab in the sidebar and then select our custom objects from our BIM DB app—Movie, Cast, Person, Review, and Company.
- Once you have selected them, drag and drop the objects so you can see them all in a clear overview on the page, like so:
Now, let's create a lookup relationship on our Movie object, called Director (assuming a movie can only have one director in our app). To do this, we need to follow these steps:
- Click the Elements tab in the sidebar and drag the lookup element onto the Movie object on the canvas.
- In the popup Create Lookup Field (Object: Movie), fill in the following information:
- Label: Director.
- Field Name (this will be automatically filled with the same as the Label field).
- Related To (select the Person object).
- Leave the Child Relationship Name and Related List label as is (it should say Movies).
- Hit Save.
- On the canvas, you should now see a new field called Director, in the Movie object, that is a direct any-to-one (Movie to Person) relation to the Person on the canvas:
This is exactly what we wanted. A movie has only one director (in our app), so that means that a Director (Person) can direct multiple movies right? Exactly!
Now, let's create a relationship between Review and Movie. What kind of relationship would we need here? Think about it for a moment:
- Can a movie have multiple reviews?
- Can one review be about multiple movies at the same time? Would that make any sense?
- Would it make sense to keep reviews about a certain movie if that movie did not exist anymore?
If your conclusion is to create a master-detail field called Movie Title on the Review object that points to the Movie object, you would be correct.
Just to be clear, a movie can have multiple reviews (I certainly hope so, because that will give us a more accurate view of whether people liked it or not). It does not make sense to create only one review for multiple movies. You are reviewing a movie and you should write a review for each movie you are assessing! When you delete a movie from the database, you would have reviews floating around without any reference to the movie the review is about. That would not make any sense either. A master-detail relationship makes sure that you always need a reference to another object on the child and that all reviews would be deleted automatically if we deleted the movie itself.
So, going ahead, just like we did previously, drag a master-detail element onto the Review object and call it Movie Title. Obviously, it will be related to our Movie object, which will automatically generate a child relationship called Reviews. In the popup, do not check the Reparentable Master Detail checkbox!
I reshuffled the objects a bit on the canvas to see all relationships in a better way, but you should have something like this now:
Okay, we still have one unrelated object in our schema—our Company object. It's feeling a bit alone without any relatives. Let's do something about that.
What I had in mind is that the Company object will contain several entity records such as production houses. I did not call the production house object, because maybe, in the future, we could have companies related to movies or other objects in a different way, for example, investing companies, much like I am anticipating with the Person object. We will have actors, actresses, producers, and directors who are all individual people. So, when I lookup The Godfather on imdb.com, I can see that it has two production companies related to that movie—Paramount Pictures and Alfran Productions. So, this means one movie can have multiple production companies and one production company can produce multiple movies! This is a many-to-many relationship, right?
The following is what we have learned about many-to-many relationships:
- You'll need a junction object.
- A junction object consists of two master-detail relationships.
- You always create the master-detail lookup field on the child object, so in our case this means we'll create the two master-detail lookup fields on the junction object.
Go ahead and create a new object called Production Company, with a master-detail field (movie title) to Movie, and a master-detail field (company name) to Company through the Schema Builder.
The result should be like the following screenshot:
Super duper, dude! You just added fields, objects, and relationships through the Schema Builder and added extra value and insights to our BIM DB app. Now, everything will make much more sense, and we will be able to get a clear view on who directed a movie, who was cast in a movie, read movie reviews, and also see what companies produced the movie.