Postgres & Sequelize - Associations

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what's going on guys welcome back to a brand new video this is web dev journey and in today's video we're going to be talking about associations and also implementing them obviously but before we get into that i i do want to mention a few things and that is i did create a few code uh already uh already did some stuff in this application because i wanted to speed this thing along guys this is going to be i'm gonna just say a very long video i'm pretty sure uh this is associations we're talking about and it's the primary thing of sql right sql databases right so yeah you could bet that associations is a a big topic to talk about and even though i'm not gonna be doing all that uh i'm not gonna be talking i mean i'm not gonna be covering everything about associations i will be covering most of the main stuff about associations but just to let you know i already did some coding so i'm gonna be walking through the code that i've done when we get to that part so first of all i got three models right here because obviously we need some models to make associations so i created some models right i got a user model we got the contact info model and we also have a tweet model and we're probably going to do like a a rundown version of a twitter like the very basic stuff so let's just get that let's just get right into it okay so in the last video in the very end of the video we talked about associations the three type associations one to one one to many and many too many and sqlize gives us four types of uh associations that we could well not could that we need to use and combine in order to make those uh one-to-one one-to-many minutes many right and these are the four types of associations that sqlites gives us has one belongs to has many and belongs to many you could kind of guess which ones which one is the one-to-one one too many and many too many so we're going to start off first of all creating a one to one an association and in this models like what what would you consider a one-to-one relationship in between these three models that we have user contact info and tweet i want you to like try to think about a one-to-one of an association also a one-to-many association and the very last a many many association between these three models right here i'm saying this so that way you could like get your head around like what is a one-to-one um when when looking into models like how would you determine what's a one-to-one what the one-to-many what they want many-to-many right so i'm just trying to get you ahead of that so anyways a one-to-one association in these three models would be user and the contact info right because one user could only have one contact info and vice versa vice versa one contact info has to belong to one user and that would that's that's what a one-to-one relationship right a one-to-one association so we're going to be creating a one-to-one with user and contact info so down here i'm gonna say one two one like i said guys we do need to do a combination of these in order to create a one two one and the only combination is these two you're gonna see it's not that hard of a combination and we're gonna be talking about like why why do we need to do a combination what is a has one and belongs to okay so first of all let's try let's just write some code right so we're gonna say user dot has one contact info contact info so this in turn gives us an association it'll be best if i actually uh i think yeah never mind i was doing some stuff i was trying to i was trying to uh you know explain to myself like how would i teach you this or uh make a tutorial about something anyways screw it that doesn't really matter so anyways so what we're trying to do right now is make an association so we have a table for users i'm going to say you for users and the table for contact info right so right now we're saying that user has one contact info basically the only model that knows that we have an association to contact info is the user model this user model is the only model that knows about the association between these two okay so let's say that i wanted to grab the contact info of user123 right which the contact info is just a phone number i didn't really explain that here by the way the contact info is literally just the id and the phone number that is it i'm not gonna be you know doing all that street and all that stuff screw that um so what what we're trying to do let's say we're trying to grab users uh user123's phone number okay phone number being uh one uh well nine nine uh eight seven all right let's say that that's his phone number so we have this association between user and contact info and we could absolutely do that through the user table we're going to say find you know user one two three and get and get a number okay and this is absolutely doable because user knows that we have an association with contact info and it's okay if user goes inside the contact info table and grabs that number because we have an association but let's say that we wanted to find a user using the phone number so we're going to say find user by a number right now this is going to be a problem because now we have the 987 number and we're trying to go inside of the user table which contact info does not have an association with user right contact info does not know we have an association with user because we didn't determine that right here we didn't see that user has an association with contact with them but it's not the other way around so this right here will throw out an error saying that we're not we're not going to be able to do that now this is where um belongs to comes into play right here what we could say is say contact info dot belongs oops dot belongs to user and this in turn gives us an association between contact info and user now now this right here this query that we run will work all right now we could actually find a user by the phone number that they give us this is why we need both of these together so that way we have a back and forth relationship between user and the contact info okay just want to clear why we needed to combine uh two of them to make a one-to-one uh an association and this is why to create a real one-to-one relationship in sqlize all right enough about that let's let's just um let's just show you how it looks like in the database all right so i'm gonna control save this right now uh i think i did an mp i did not do an mpa start hold on oh it's sorry has capitalize o control save and there you go so let's see let's see how this looks like in our uh database the tables how the tables look like so if i go to pg admin i refresh this all right now we're going to have three tables obviously because contact info tweets and users let's go to users and see what's going on here users should be the same thing as last time except for password no password right and we don't have anything now contact info is something different i do need to explain that a little bit more the contact info has the id that we talked about right we have the um over here let's check over here we have the id the phone number and then we also have the time step so created that and updated that right but as you see god as you see right here we have all that but we also have this extra column here which is the user id where did that come from now when you make an association between uh tables between two tables one of them is going to have a foreign key this foreign key is basically the reference point of like where it's trying to reference right so in this case contact info is trying to reference something inside of the user because contact info belongs to user basically the contact info is going to reference a user inside of the user table therefore with this one right here how we set it up the contact info is going to have a foreign key that foreign key being the user id and it creates it for us automatically as you saw right now user id this is a foreign key this is important because like i said let's go back into this little model right here like i said if you try to find a user with one two three and try to grab the number for that user right well since 987 has that foreign key of user id guess what's in what's stored inside of the user id column one two three so now we could say that hey we're trying to grab users user one two three and trying to grab their phone number so here we're gonna look at the foreign key and say that hey look at the user id oh this is one two three so 987 is the phone number for user one two three and vice versa right if we try to find a user uh by their number we're going to look at what here and say that hey we're trying to find this number 987 and okay it's referencing the user id of one two three so let's look inside of the user table and see okay here's the user one two three and this is uh you know that user so it's very very important to have a foreign key and guys if you do not know where the foreign key should be placed as you saw the user you the user table did not have a foreign key the only uh table that had a foreign key was the contact info right if i go back over here go to users you can see that the user does not have a foreign key at all in here right but the contact info does if you're trying to figure out where a foreign key should be placed you could ask yourself a a pretty simple question and this is the way i thought about it when i was trying to create a foreign key right so the way that i asked it was like which which model could live by itself which model could live by itself is it the user model or the contact info that that could live by itself the user model obviously could live by itself right because it doesn't need anything the contact info in the other hand needs referencing to the user it needs to have it need to be tied back to a user so the contact info cannot live by itself it needs some type of reference back to the user right so in this case since the contact info cannot live by itself it needs that foreign key inside of it so this is how you would i guess um decide where the foreign key if you're new to this where a foreign key should be placed uh it should be placed inside of the table that needs referencing back that cannot live by itself and we'll talk about that a little bit more okay and i will bring up that question once we do other relationships but basically that's pretty much it for a one-to-one relationship let's now populate it with some stuff all right with a user and also the contact info for the user so that way you can see how you would do these things okay so like i said uh let's go inside now that we have our models all set up for the one to one let's go inside the the services and go to user service because this is where i did it i mean not now i did it but in the user service our create user is basically the same right first name that's an email we're going to get this all from the body the json data and then we also need to worry about the contact info right so i created another service for our contact service which is going to create the contact info as you see right here and it just takes basically a phone and the user id and this is going to we need the user id like i said because remember we need to populate that reference key or that foreign key in order to reference back to an absolute user right so we need that user id and we're going to be creating a uh a model with a phone and user id now some people are going to get a little bit confused here because we're passing in the user id into the model contact info even though inside of the contact info we don't have that user id as an attribute right here we don't have it so where is that coming from like i said guys once you create that table that user id is automatically generated for you so whether you like it or not that user id column is inside that contact info table so you need to populate it with a reference key and by the way to make sure you do not leave that uh null you don't leave that rep or foreign key null what we want to do is right here we want to give it some options yes we could actually add some options with our associations and it's going to be a simple a simple um option that we're going to add so in here add in your brackets right uh and we want to add an option for the foreign key and that is we want the type of this the type of the foreign key to be data types dot uuid since the ids of these things are uuids i want the foreign key to also be a uuid type also we don't want this to be null so allow now is going to be false we don't want it to be false this is going to make extra sure that that field is populated with with something all right so control save make sure you do this just a little bit of um good practice to do that also if you don't like the name user id for your foreign key you could absolutely rename it all you have to do is just say name right here and then name it whatever you want uh i don't wanna name anything i like it the way they name it so i'm just leave it like that okay all right so anyways on our contact info now you see that we created a contact info or a function that's going to be creating a contact info so now inside of our routes our user routes our index this is where we're going to be creating a user and also creating a contact info because this is where we're going to be sending all that info right so right here obviously we're grabbing the user we're going to be user service.create user passing in the rec.body and then here we're going to be creating a contact info passing in the phone number and also the user id that we got from user so i'm using the user that we got back and passing in the user.id that comes with that user and all we're going to be doing is receiving the user and the contact info okay so let's see how this works this shouldn't be new guys that's why i'm speeding along this this and the user services should not be new to you guys that's why i'm just like showing you really quick how i did this and how i uh put it into the route and that's it so let's go to postman and create a user i already have something along those lines so let me see create user create we have a wdjdo wdaw.com and then phone number is this random number let's send this and look at this we get our user and also our contact info and you're gonna see that our user id inside of the contact info which is our foreign key is exactly the same as the the id as the user now we have a reference point back to the user right which is what we wanted okay all right now let's create a one too many relationship going back into the index.js for our models and in here i'm basically going to copy this because it's going to be basically the same thing so i want you to tell me one too many and one too many actually combines has many and belongs to like i said we need the belongs to no matter what and also there has many but between the models what do you think has a a one-to-many relationship between these models that's pretty simple right a user could have many tweets that's a one-to-many relationship or association right so all we have to do here as you guess we're gonna say user dot has many tweets or tweet that's our name of the model tweet and also tweet belongs to user not that hard right it's basically the same thing as the one to one man the many the the many too many is going to get complicated so i want to save that so i'm not going to go over this because this is pretty much the same thing as a one to one but instead of one to one it's one to many obviously right and we can actually check out the uh pg admin to see how the table looks like so instead of the contact info if i run this you can see that that actually got there so if you go to the um what's it called the tweet tweets table right we're gonna see id title description that's exactly what we have for our model id title description plus a time stamp so plus the time step and obviously along these we're gonna have the user id as our foreign key so let's try to populate this with some uh tweets right so let's go to oh well first of all let's see how i did it so let's go to our code so inside of our services i decreed a tweet service so we go inside that this is going to be creating some tweets and like i said we need to pass in our user id that user id that's going to be referencing to you right our foreign key we also need to pass in the title and description and then once we have that we're going to create a tweet dot create and pass in the user id title and description and then just simply return the tweet that we saved now if i go inside of my routes inside the tweets route i did create a tweets route the index.js we did it create a create route for our tweets and first of all i do need to find the correct user so i just did the find one user which is going to be let me see the final one user is going to be me right so i'm the current user which you should probably want to have the current user anyways where's my uh right here so i'm gonna grab the user meet my user right and grab their id so that way we could pass in the that user id and also the title and description okay so let's see if this works or not so let's go to postman and i already did already obviously right we're gonna go to postman and we're gonna go to tweet create right here and as you can see i'm only passing the title description because we're already grabbing the the user id from trying to find that one user so if i hit send right here we should get our user and our tweet right and let's do a few more let's just do a few more let's go to um let's do this one i'm gonna say v2 yep v2 and let's do one more which it should be v3 and there it is v3 bam right there so now let's check out our let's check our uh pg admin let's hit this one more time and you're gonna see that i do have three of them you know title description whatever and the user id our reference key is not going to be the same it should be which is f3e and if i go to my users my user id is f3 e awesome right and again guys we could have asked right here that simple question if you did not know where that foreign key should be placed which one could live without the other right which one could live by itself and which one came obviously user could live by itself and tweet cannot it needs a user associated with it so in that case uh foreign key should be inside of tweet all right now let's focus on the many-to-many relationship this is going to be the most complicated one all right guys so i want you to pay if you don't pay attention at least pay attention to this because this is the most complicated one out of all of them because like in the last video i said that you cannot like for instance these two actually you could make relationships associations with only two tables right with the many too many it doesn't work that way you need three tables one being the junction table or the junction model and then uh the join table okay so again i will walk through like what is a many too many so that way you have a clear picture of what it is even though i explained in the last video so it's going to be very very quick right so in this case i'm not going to be doing this model right now so let's say that we have um a table of actors and we have another table of movies right so um actors could be inside of many many movies right and also movies could have many many actors this is a many-to-many relationship but this is the shorthand version of the many many relationship right the long hand version would be we have a actors and then we have a movies actors and then right in the middle we have our junction model or our join table which we're going to be calling a underscore m so that way we know right so in this case and it will it will make more sense if we had ids right so let's say we have uh id for actor wait what hold on sorry about that i was having some technical difficulties man my pin wouldn't be working yeah i actually bought a pin now guys so yeah anyways all right so it'll be much more easier easier if i explain like this so obviously this right here is going to have a um an id and also movies it will have its own id correct but since now we're having this middle man right here our join table this is going to have reference to and i'm going to make two columns right here it's going to have reference to our uh a id our actors id and our movies id right so actor one two three let's just say one two three could be referencing a movie with the id of uh let's say uh four five six right so four five six just like so right so now this is going to have a one to many relationship because one actor could have a many relationship with the uh the table this table right here and also this movies could have a one too many relationship with this table right here our join table okay yes we could get stuff out again right but this right here is going to be our many too many relationship our association okay now i have to add in the the id this is very important this is what it does right so the actors table has an id movies table is an id and once it creates our join table our junction model is going to automatically uh create a actor's id inside of here and our movies id inside of here or foreign keys our foreign keys okay it's going to create foreign keys for us i wanted to show you this because this is going to make much more sense once we start coding because when it comes to actors and movies or when it comes to uh having a many many relationship between two models it's very easy so let's just make it in this in this case right here let's do a movie actors and movies relationship let's let's assume that i have an actor's models and a uh movies model okay so right here let's try to do a let's grab this and let's try to do a many-to-many relationship which only has it only uses it belongs to it this one does not need a combination it just uses it belongs to so in this case if we did have a knock there's model and a movies model we would be saying hey um we could say actor dot no man dot belongs to many make sure to spell this right because you're going to get errors belongs to many um movies right or movie actor belongs to many movie also we have to say like like i said you still need to do a two-way round trip on this right because i said actor right now what we did right here actor belongs to many right here actor belongs to many movie right now we need to do movies could have many actors so in this case it's just the opposite so movie oops movie dot belongs to many actor right so these two lines basically did this now with belongs to many with a many many association in sqlite there's a couple of options that you need to add um this is required okay like i said we need this junction table right here this junction model obviously we do not have an actor's uh actors underscore movies model this is going to be created for us automatically and in order for it to do that we do need to say hey we want to make a many many relationship between actors and models are actors and movies and we want it to go through a junction table named whatever right so in that in that case we're going to add some options right here and we're going and this is where we're going to say we want that to go through the model um well in this case let's just name it um act doors actor underscore or actors underscore movies all right so this right here is going to create this junction table for us automatically and yes we do need to have both of them oops there we go now right here this actually creates this right here this model right here a a mini relationship with our junction model our join table as actors movies and inside of actors and movies is going to be creating that reference key of actors id in here and our movies id in here okay but now why did i show you this with models that i did not have the reason why is because we could actually make a many-to-many relationship using only one model that we have and that's what we're going to be doing and you might be scratching your hair like what how can we how what how it's exactly the same thing we're only using one one model and i'm i'm gonna show you right now so what's our many many relationship that we're going to be creating uh so in this case users right i'm actually i'm gonna have to draw this out guys so that way you could uh see what's going on right so i'm gonna leave this here so that way you can still reference it right but we're gonna be using users the table users all right so users could actually follow many many users other users right and other users could follow you right other users many other users could follow you so this right here within the user table it could be a many-to-many relationship with itself right because i could since i'm a user i could follow many users and other users could follow me right this is a many-to-many relationship now this is exactly the same thing over here but only using one model so i'm gonna just say equals right here this and this right so now we need to convert this into this little model right here so how will we do that well same thing right user we're gonna have a user model right here obviously and we're gonna have another user model over here right and they they both have the exact same id right the user id id and id right but since we're going to be creating a user table or a join table sorry a join table or a junction model we're going to say this we're going to call this followed or um yeah i'm going to say follow this is going to be this table is going to be named follow all right so in this case once we create a one-to-many relationship right here and a one-to-many relationship right here this is where it's going to confuse people because over here what we did is they gave a reference keys of actor id and a reference key to the model id or the movie id right but over here since we're both using the user how how is that going to fit right here how how does that work right how does that work we can't have two user id in here because that's not going to work out and here comes the options right i said this in the and like and i think in the one-to-one relationship we could actually name our foreign keys okay so let's take this into account for this user right here for this user model the user id or the foreign key name is literally going to be user id so i'm going to say uid that's kind of hard to read so let me try to make a little bit bigger so that way you can see what's going on okay all right let's uh let's try to make this a little bit bigger so that way you can see what's going on one too many one too many whatever all right so this user model coming in is going to literally have a foreign key over here called user id for right now which is a uid and in my head i want it to be that this user id is the user that's following so this user id is going to follow another user so in this case this user model we're going to name this as a followed id i'm literally going to call it i'm going to say followed followed i d and that's going to be right here right so in this case let me say me so one two three user one two three followed one two three followed four five six right also one two three followed 987 also let's say that 987 user id 987 followed um i don't know four five six right in this kind of sense in this kind of way it does make sense right so that's why i wanted to have a user id and also a a followed id right here so that way we could say that this user followed this user i know it kind of makes sense it makes sense i hope it does make sense but this is what we're going to be doing right and like i said it's exactly the same thing as this over here this correlates or this uh equals to this but it's just different right because we're adding specific uh foreign keys so let's take this into consideration right here right here okay uh then i oh right here i forgot to do the um percent so anyways so in this case instead of actors and movies it's literally just user let me just copy this belongs to many user and user belongs to many user again like i said we still need these right here that's going to be curling so let's focus on this part right here this one right here let me actually circle it or color it into another one we're going to be focusing right here first of all this right here okay let's focus on this little bit piece right here so this is going to be our top row right here and we need to give it a foreign key of user id this is where you know having a diagram or drawing out like schemas and all that helps out so much so that way you understand it a little bit better so anyways we're going to be working on this yellow section first of all and that's going to be our top row right here now because we are using just one model it's going to get a little bit of complex on the options that we're going to be adding okay it would have been so much easier if we just had two models but i wanted to show you this way as well where you could use it as one so that way because you're going to be doing this as well let's face it you're going to be using one model as a one too many many too many relationship some somewhere through your life you're going to be doing this i know for a fact you're going to be doing this so anyways all right so since these are basically the same thing and sqlite doesn't know how to determine like what's what's what right so i want to give them an alias i want to give them another name so instead of having this we're going to say i want to reference this i'm going to say as i want to reference this as user so this first one is literally going to be the same thing it's going to be the user the user uh we're going to be referencing this as the user model and by default guys by default the foreign key is going to be generated using the alias that we gave it okay so in this case since our alias is user the foreign key that it's automatically automatically going to give us is going to be the user id right if it didn't and if you really want to make sure that the foreign key is going to be user id you could literally just say foreign key and let's just do it right now right let's just do it foreign key we want it to be let's say user id and that completes this section right we have our user model right here and we also have the user id inside of the oh and by the way we want this table to be called follow so we're going to just say or we could say user underscore follow but i'm going to say follow follow like that right we're going to have this user id our foreign key user id inside of the follow table that it's automatically going to create for us all right now that we have this section this part of it done now let's do let's get my blue one out now let's do this piece right here all right this other half okay all right so in this one and i'm trying to uh let me actually get rid of this since you already know what's going on over here so let me get rid of this so that way you can see a little bit more clear all right all right all right all right so on this half is going to be the second bottom half obviously so we want um to rename this as well because it's still being used as user so we need to give it an alias right and our alias like i said so since we're going to be calling it the followed id we're going to just say as followed oops let me uh put in quotes as followed and we're going to give it the foreign key let me copy this there we go we're going to give it the foreign key of like we said right here followed id so followed i d or id and it's going to go through our join table which is the follow table let's control save this we shouldn't get any errors where's my what okay see that we didn't get any errors so this works so let's see let's see what this created in our uh pg admin all right let's check that out let me actually get rid of all this so i just wanted to like i hope you understood all this coming to this okay i hope you understood that 100 i tried to make this as simple as i could but now let's go to pg admin and see what we just created what monstrosity did we create if i refresh the table section refresh you're gonna see that we have a follow table here's our join table okay if i uh over here select everything from follow you're gonna see that hey look this is exactly exactly what we wanted we have the created ad and the updated at which is fine because that came from this timestamp but look at this check this out we have two primary keys user id and followed id now let's put this into use let's try to figure this out let's try to actually make myself follow some other people okay so in this case since my users i still only have one user i could technically follow myself but let's not let's create some other users um we're going to say uh danny danny blake is that is that how you do it danny i want you to get their emails as well danny blake danny blake.com all right there we go we got there we created a user and a contact info let's create one more let's create a tom um tom dane is dude i'm not even sure these are real names dane is that is that even the last i don't know man like i don't know i don't have friends so we have we have two more users okay so let's hit this all right you can see that we have two more users so myself wj wants to follow both danny and tom so let's try to uh see the code first of all let's see the code what i wrote so that way what we could do right so let's go in our code so now that we have this many-to-many relationship let's see how we could actually you know start following people so inside of users the services user service all the way down i created a method called follow user right so we're going to get a current user which is going to be me because i'm using the find one user which is going to return wdj the con the user wdj so this is a current user right and then we want to follow this is to follow user right we want to follow this user which i'm trying to find someone called danny so now we have the current user and the person we want to follow so how do we do this right and you might be seeing this next bit of code and you're like okay where did this ad user come from and that's a great question i did not talk about this at all i forgot actually i forgot about this so when you create associations right when you create associations they give you special methods that you could use with them the magic their magic man they make your life so much more easier okay but they do give you special methods that you create matter of fact hold on let me actually pause it and pull up the sequel lights uh dock so that way you can see it alright so i have this equalized docs and i found it right here okay so as you see they they create special methods right so in this case this is a food that has one bar this is a one-to-one relationship or association right and it belongs to whatever but check this out this is the special method that it created so the foo instance dot get bar set bar create bar it's whatever you name the the the model as so in this case it's bar so it's going to be get bar set bar create bar if it was cat it would be git cat set cat create cat okay over here we have a food that has many bar in our case it's user.has mini user but like i said we did create aliases uh for those users so in this case it would be uh let's take one for example oh wait this is the uh sorry this is the has many sorry about that this is the has many which is a one to many so it's still the same thing right but it gives you a little bit more methods that you could use right so you have the uh has bar add bar remove bar and all that stuff and here's the special methods that the um belongs to many go through right in this case the foo instance dot get bar account bars um hasbar whatever it doesn't really matter all right but this right here has like i said once you create associations they give you special methods and i'm just using one of those special methods as you can see right here i'm adding the the the dot add user and that is a uh right here add bar that that is a special method you could use but like i said the bar comes from the name of the the name of the model in this case you could probably guess what model we're using here right the add user we are using this top model because we created the alias as user okay if i wanted to do uh something with the bottom uh bottom right here this bottom association i would say you know add followed or get followed or you know set followed but in this case since i want my current user to add a uh to add a another user or to add the person they're going to follow i want to be doing this right so it's going to be user dot add user and then the followed id or the person that i'm trying to follows id i hope that made sense right so i'm using where is it at right here so i'm using that special method add user so that way we could do that without you know doing all those things and this is literally going to pull out the id from the to user uh whatever we got back it's going to pull out the id from it and just store it in the you know proper place and then all we're going to say is current user and get user get user like i said is another thing so i just want to grab all the users that i'm following so far so this is why i say get user and that's pretty much it right it's pretty pretty simple right there and that's that's because of the magic of those special methods okay but anyways now that we have this method right here follow user i did use it in my user index right here and i think it was in the very last yeah right here so it's going to be slash user follow route and all i'm doing is calling that follow user and we're going to get a we're going to be sent back a followed list dude i hate when my printer just like starts to uh update on its own it it gives me the jumps all right so anyways let's try this out okay we don't have to enter anything all we have to hit is hit this route because we're doing everything right here refining we're grabbing our user or i'm grabbing myself and then i'm trying to follow danny i'm doing everything hard-coded okay so anyway let's just hit that route so i think i already have that somewhere around here right here follow it there we go follow you don't have to send anything let's just hit it hopefully yeah okay so why did we get an empty array all right so initially i'm not following anybody so it's going to return an empty array and don't worry you'll see what i'm talking about in a bit all right so anyways let's check out our uh pg admin and see what's going on there so if i go to over here and then hit this again what do you think is going to happen wait what did what the man did did i made a mistake i swear oh no follow there we go check this out check this out i wanna so this is going to make more sense right so user id let me see f3e is that me yeah f3 is me i try to follow 0 6b right so user id f3e followed uh 0 6b which is correct this is danny right here danny uh and now this model right here this this model is starting to make this table right here is starting to make much more sense right the way we did this let's try to fat let's try to since i'm already following danny let's try to follow um tom he's trying to follow tom alright so let's go tom control save let's hit this route again now we're going to try to follow tom let's hit this but check this out now we have an array that's because now we started to follow danny and it's going to give me that one uh back it's going to give me that one back right this is something different this is another method but don't worry about it we're gonna like i said man uh we're gonna be talking about this much more once we start doing a lot of things with postgres equalize but anyways so let's go back into our pg admin and check that out if i hit play again you're going to see that user id f3e which is me is now following 9b9 which is uh tom 9b9 is tom right cool now this is a many-to-many relationship association now we could figure out uh who i'm following all the people that i'm following and we could also figure out who's all following danny or who's all following tom right that's going to be great now and that's pretty much it for this video guys because we did a lot and we talked about a lot of the things like i said this video was going to be long and this is just associations we still need to do a lot more right we still need to do a lot more with associations but that's going to come in in the uh next videos that are coming up guys don't worry about it like we still got a lot a long way to go until it equalizes over or sequalizing postgres right but i will leave a link to this uh to this right here so that way you could see the associations and uh all the way in the below all the way in the uh bottom it has all those uh methods of special methods that you could use and you can see how to use them all those things we are going to be talking about some of them throughout the course i mean not of course sorry i keep on saying course throughout the series but not all of them um so i would highly encourage you guys to look at this and see what's going on but anyways this was a long video guys so i hope you learned something i hope you you know um read the docs for yourself so that way you won't be confused because i tried to explain this as best as i could okay but um yeah i mean i really just hope that you actually got something from this video and if you did please hit that like uh subscribe to my channel and also comment down below also if you want to support me in any other way there are going to be links down below so that for my patreon and also uh ways that you could buy me coffee a cup of coffee and speaking of coffee i need some coffee right now so i'm gonna be heading downstairs making myself a copy so without further ado guys i will see you in what why did i say without further ado i'm sorry i really appreciate you guys watching my videos and supporting me so thank you guys and i will see you in the next video bye
Info
Channel: Web Dev Journey
Views: 12,801
Rating: undefined out of 5
Keywords: node js, nodejs, sequelize, postgresql, postgres, pgadmin, pgadmin4, docker, sequelize models, models, CRUD, getters and setters, one-to-one, one-to-many, many-to-many, Paranoid, Validations, Associations
Id: A1dAHmzpcX0
Channel Id: undefined
Length: 48min 58sec (2938 seconds)
Published: Thu Mar 11 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.