Playing Next Lesson In
seconds

Transcript

  1. So far within our database schema, we've worked with everything except for our watchlist table. Our watchlist is bound to our users and our movies table,

  2. and it's gonna keep track of movies an individual user is interested in watching. So our user ID and movie ID there keeps track of that relationship, and it will be a many to many relationship

  3. because we can have many users wanting to watch many movies, and many movies with many users wanting to watch them. Apart from those two columns, we have our standard ID created at and updated at columns,

  4. but we also have this watched at timestamp as well. And we're gonna use this to allow a user to keep track of whether or not they've watched a movie, and we're keeping that as a timestamp

  5. just in case we might ever be interested in showing when exactly they watched it. So if this does not have a value, then it means that they didn't watch it, and if it does have a time within there, then it means that they watched it.

  6. Now, unlike with our crew movie and cast movie tables where we didn't have a model for either one of those, and instead we worked directly through the many to many relationship between the movie and Cineasts table.

  7. For our watch list, we are going to create a model for it, despite it being a many to many relationship. And then within the model itself, we'll define the relationship between the movies and the watch list,

  8. and the watch list and the users individually without actually using that many to many type. This is a great approach whenever your pivot table is the focal point of the relationship,

  9. where we can work directly with a watch list model rather than working through the many to many relationship on either side of that. And it also gives us a chance to show an alternative way

  10. to define a many to many relationship as well. So let's do node ace make, and we'll create a model specifically for our watch list.

  11. We'll also make a migration and a controller using hyphen MC there, run that. Okay, we can clear that out, jump into our text editor, and let's first dive into our database,

  12. migrations, create watch list table migration. So everything by default here is a okay. In addition to our ID, we're gonna want a table integer for our user ID.

  13. This will be unsigned references, our users.id to create that foreign key relationship between our watch list and our user.

  14. This will be not nullable and on delete, we'll want to cascade the deletion into any of these records. We can go ahead and give this line a copy because we're gonna want that to be relatively similar

  15. for our movie ID there as well. The only difference is going to be instead of users, it's going to be for our movies. Lastly, we have one additional timestamp,

  16. our table timestamp watched. All right, that should do it for our migration. We can go ahead and jump back into our terminal and run node ace migration run there. Okey-doke, clear that out.

  17. NPM run dev to go ahead and put our server backup, hide that away. And now let's jump into our watch list model and get the columns defined here as well. So we have an at column, declare.

  18. We have our user ID of type number. We also have a column for our movie ID. That too is of type number. And then we have the watched at.

  19. So we'll do at column. We'll get that as a date time if it exists. And then we'll declare that as watched at

  20. as a type of date time or null since it is optional. Now, since we have a model specifically for this pivot table of our watch list,

  21. instead of using the many to many relationship type in either our user or our movie model, instead we'll want to create this as a one to many relationship. Meaning within our watch list table,

  22. this will be belongs to for both our user, declare user belongs to type of user. Scroll down a little bit further here and we'll do the exact same thing for our movie.

  23. So belongs to movie, declare movie, belongs to type of movie. And that reads quickly there, of course, is just because we need to add an import type for belongs to.

  24. So that's the relationship within our actual watch list model. What about within our user and movie model? Well, within our user, let's go ahead and scroll on down to where we have our relationships.

  25. And rather than a has one or belongs to on this side, it's just gonna be a has many and we'll bind it directly to our watch list model. So we'll do at has many there

  26. and point it to our watch list model. Declare watch lists as many type of watch list. Now watch lists may not be the most appropriate name

  27. because in display sense, the user is really just gonna have one list, it's not multiple lists. So in that case, maybe we will just name this watch list rather than watch lists,

  28. despite it being a has many relationship, just to stifle any potential confusion there. Then on our movie side, we're gonna do pretty much the exact same thing. So we'll just need to scroll

  29. to where we have our relationships. We'll do these above the many to many in between our belongs to here. And actually we can go ahead and just copy this verbatim from our users

  30. and go back into our movie, paste it in. And we just need to hit command dot there to get our quick fix options and add in our missing imports. So command dot there, command dot here.

  31. There's also an add all missing imports. So I suppose we could go ahead and click that there as well. Give that a save. And now we have our relationships bound.

  32. And despite our watch list being an actual pivot table between our users and movies, we've instead defined it as a one to many relationship between our movies and watch lists

  33. and a one to many relationship between our users and watch lists, which gives us an alternative view at how we can go about many to many relationships within our application. Structurally, it's all still just the same thing.

  34. It's just how we're treating it that's different.

An Alternative Approach to Many-To-Many Relationships

@tomgobich
Published by
@tomgobich
In This Lesson

Learn an alternative approach to many-to-many relationships that allows you to work directly with the pivot table as a model.

Database Diagram

Database Diagram

Join the Discussion 4 comments

Create a free account to join in on the discussion
  1. @codthing

    If the GitHub repository includes the relationship diagram from the video's opening, it would be helpful for learning.

    1
    1. Responding to codthing
      @tomgobich

      Hi codthing!

      Thank you for the great suggestion! You can now find the database diagram within readme of the repository and the repository's public directory!

      1
      1. Responding to tomgobich
        @codthing

        thank you tomgobich

        1
        1. Responding to codthing
          @tomgobich

          Anytime! Thank you, again, for the great suggestion!

          0