00:00
(upbeat music)
00:02
So far within our database schema,
00:06
we've worked with everything except for our watchlist table.
00:08
Our watchlist is bound to our users and our movies table,
00:12
and it's gonna keep track of movies
00:14
an individual user is interested in watching.
00:16
So our user ID and movie ID there
00:18
keeps track of that relationship,
00:19
and it will be a many to many relationship
00:21
because we can have many users
00:23
wanting to watch many movies,
00:24
and many movies with many users wanting to watch them.
00:26
Apart from those two columns,
00:27
we have our standard ID created at and updated at columns,
00:30
but we also have this watched at timestamp as well.
00:33
And we're gonna use this to allow a user
00:34
to keep track of whether or not they've watched a movie,
00:37
and we're keeping that as a timestamp
00:38
just in case we might ever be interested
00:41
in showing when exactly they watched it.
00:42
So if this does not have a value,
00:44
then it means that they didn't watch it,
00:45
and if it does have a time within there,
00:46
then it means that they watched it.
00:48
Now, unlike with our crew movie and cast movie tables
00:50
where we didn't have a model for either one of those,
00:52
and instead we worked directly
00:53
through the many to many relationship
00:54
between the movie and Cineasts table.
00:56
For our watch list, we are going to create a model for it,
00:59
despite it being a many to many relationship.
01:02
And then within the model itself,
01:03
we'll define the relationship
01:05
between the movies and the watch list,
01:06
and the watch list and the users individually
01:08
without actually using that many to many type.
01:10
This is a great approach whenever your pivot table
01:12
is the focal point of the relationship,
01:14
where we can work directly with a watch list model
01:17
rather than working through the many to many relationship
01:19
on either side of that.
01:20
And it also gives us a chance to show an alternative way
01:23
to define a many to many relationship as well.
01:25
So let's do node ace make,
01:28
and we'll create a model specifically for our watch list.
01:31
We'll also make a migration and a controller
01:33
using hyphen MC there, run that.
01:34
Okay, we can clear that out, jump into our text editor,
01:37
and let's first dive into our database,
01:39
migrations, create watch list table migration.
01:42
So everything by default here is a okay.
01:44
In addition to our ID,
01:45
we're gonna want a table integer for our user ID.
01:48
This will be unsigned references,
01:51
our users.id to create that foreign key relationship
01:54
between our watch list and our user.
01:56
This will be not nullable and on delete,
01:59
we'll want to cascade the deletion
02:00
into any of these records.
02:02
We can go ahead and give this line a copy
02:03
because we're gonna want that to be relatively similar
02:05
for our movie ID there as well.
02:08
The only difference is going to be instead of users,
02:10
it's going to be for our movies.
02:11
Lastly, we have one additional timestamp,
02:13
our table timestamp watched.
02:16
All right, that should do it for our migration.
02:18
We can go ahead and jump back into our terminal
02:19
and run node ace migration run there.
02:22
Okey-doke, clear that out.
02:23
NPM run dev to go ahead and put our server backup,
02:25
hide that away.
02:26
And now let's jump into our watch list model
02:28
and get the columns defined here as well.
02:30
So we have an at column, declare.
02:32
We have our user ID of type number.
02:34
We also have a column for our movie ID.
02:37
That too is of type number.
02:39
And then we have the watched at.
02:40
So we'll do at column.
02:42
We'll get that as a date time if it exists.
02:44
And then we'll declare that as watched at
02:47
as a type of date time or null since it is optional.
02:51
Now, since we have a model specifically
02:53
for this pivot table of our watch list,
02:55
instead of using the many to many relationship type
02:58
in either our user or our movie model,
03:00
instead we'll want to create this
03:01
as a one to many relationship.
03:03
Meaning within our watch list table,
03:04
this will be belongs to for both our user,
03:07
declare user belongs to type of user.
03:11
Scroll down a little bit further here
03:12
and we'll do the exact same thing for our movie.
03:14
So belongs to movie, declare movie,
03:17
belongs to type of movie.
03:19
And that reads quickly there, of course,
03:20
is just because we need to add an import type
03:23
for belongs to.
03:24
So that's the relationship within our actual watch list model.
03:26
What about within our user and movie model?
03:28
Well, within our user, let's go ahead and scroll on down
03:31
to where we have our relationships.
03:32
And rather than a has one or belongs to on this side,
03:35
it's just gonna be a has many
03:37
and we'll bind it directly to our watch list model.
03:39
So we'll do at has many there
03:42
and point it to our watch list model.
03:44
Declare watch lists as many type of watch list.
03:48
Now watch lists may not be the most appropriate name
03:51
because in display sense,
03:52
the user is really just gonna have one list,
03:54
it's not multiple lists.
03:56
So in that case, maybe we will just name this watch list
03:58
rather than watch lists,
04:00
despite it being a has many relationship,
04:01
just to stifle any potential confusion there.
04:04
Then on our movie side,
04:05
we're gonna do pretty much the exact same thing.
04:07
So we'll just need to scroll
04:09
to where we have our relationships.
04:10
We'll do these above the many to many
04:12
in between our belongs to here.
04:13
And actually we can go ahead
04:15
and just copy this verbatim from our users
04:17
and go back into our movie, paste it in.
04:19
And we just need to hit command dot there
04:22
to get our quick fix options
04:23
and add in our missing imports.
04:25
So command dot there, command dot here.
04:27
There's also an add all missing imports.
04:29
So I suppose we could go ahead and click that there as well.
04:31
Give that a save.
04:32
And now we have our relationships bound.
04:34
And despite our watch list being an actual pivot table
04:38
between our users and movies,
04:39
we've instead defined it as a one to many relationship
04:42
between our movies and watch lists
04:44
and a one to many relationship
04:45
between our users and watch lists,
04:46
which gives us an alternative view
04:48
at how we can go about many to many relationships
04:50
within our application.
04:51
Structurally, it's all still just the same thing.
04:53
It's just how we're treating it that's different.
Join The Discussion! (4 Comments)
Please sign in or sign up for free to join in on the dicussion.
codthing
If the GitHub repository includes the relationship diagram from the video's opening, it would be helpful for learning.
Please sign in or sign up for free to reply
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!Please sign in or sign up for free to reply
codthing
thank you tomgobich
Please sign in or sign up for free to reply
tomgobich
Anytime! Thank you, again, for the great suggestion!
Please sign in or sign up for free to reply