Database Relationships | One To Many & Many to Many | Django (3.0) Crash Course Tutorials (pt 6)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys in this video we're gonna discuss two different kinds of data based relationships we're first gonna cover one-to-many relationships and then we'll go into many-to-many relationships this is part of a jingle crash course series so for anybody here not part of that series the first half of this video should still be relevant to you because we are just gonna go over the concepts of what's going on here and for my Django viewers we're gonna cover the concepts first but in the second half we'll actually go into implementing this into Django and how it all works the first relationship we're gonna discuss is this one-to-many relationship and the example I like to think of is a customer to an order relationship so in this example we have a single customer that has placed three different orders so a customer can place as many orders as they would like but a single order can only have reference to one customer and this creates a one customer too many orders relationship a one-to-many relationship and in the database this is how it's going to look so we have two tables here so we have a customers table and an orders table every single time one of these customers places in order unique order is created but that customer ID is referenced as a single column in that orders table so in this case when John placed an order his ID was referenced right here in this basketball order and when John place another order for a barbeque grill his ID was stored again and this goes on for when Eric places an order and when Tim places an order so that's how a database stores a one-to-many relationship now to explain a many-to-many relationship I'm going to use a different concept here so in this example I'm gonna have it's going to be a store that we built out and I'm gonna have products here that the store carries and each product can have some tags attached to it so for example I'm sure we've all seen this on websites when we're shopping that product may have certain tags in it like outdoor sporting or whatever that may be and that creates just better search functionality and easier to understand what that product is so in this case we have three different tags so this is its own table right here and products is its own so if we had a ball in the store and we wanted to reference sports as a tag we can do that we can create that relationship but the problem is is if this is a one-to-many relationships we may also want to use we may also want to add summer as a tag because a ball could be an outdoor event something related to summer in this case we can't do it because it's a mini to one relationship we can only give it one tag so the solution in this case is to create a mini to many relationship and in this case ball can reference a tag and a sorry a ball can reference sporting the sporting tag and the ball can also reference the summer tag and then we can go ahead and get the barbecue grill product here and we can reference summer and we can reference kitchen because it's somewhat related and this just means that a tag can be referenced to multiple products so attack can have many products but yeah a product can also have many tags so it's a many-to-many relationship and I'll just explain this by going into the database design now and in here and I just changed the color so it's more visible the Green was kind of hard to see when we got here but in this case we have again I just took that layout I stole it from here and moved it into this picture and this is what the database is gonna look like so we're gonna have our tags table and we're gonna have our products table so in this case instead of having references in that table because it's a many-to-many relationship by default we need to create another table for this and if you're a Django user this gets created for you so don't worry about this is just gonna show you the layout but we get an intermediary table and what this table does is it stores the ID reference to both tables here so in this case when we created the relationship between sports and ball so right here this relationship this table grabbed the idea of sports one and it grabbed the idea of ball and it put it into here and it says hey there's some kind of relationship here and then when we move to summer so right here we grab the tag ID of two and we said hey that's also related to balls so we grabbed ball and put it right here so 2 & 1 and this just goes on the list and that's how an intermediary table works between the two to actually create that relationship so that explains the concept of how one-to-many relationships work and many-to-many relationships work now we're actually gonna go into Django and go ahead and add this tags listed this tags model and we'll actually build it in and we'll get to see how we can query these in the next video after this so making these relationships work and Django is actually very easy to do and if you're coming here from the last video in the series you remember that we made this order's model right here but we never set the customer attribute and the product attribute and that's because we wanted customer to be a reference to a parent right here so we wanted a one-to-many relationship and for product we also wanted a one-to-many relationship to the product right here and the reason we wanted that is because we don't want to have to set the string value every single time a product is ordered we don't have to manually add in that price or the name so in this case we want to reference a product in the store so we're not having to put that information and store it within the orders so to create that relationship all we need to do is do models dot foreign key and in here we just need to add whatever model we want to be the parent of this model right here so in this case we're gonna set that to customer and we'll just go ahead and set the no value to true again because we don't want to have any issues when we're adding items to our database and maybe forget to add a customer but the last thing we need to do in this foreign key right here is add an on delete method and what this is is basically saying that whenever a customer is deleted because it has a child of order what do we do with this order right here do we want to cancel it or do we want to just set the the value of customer to blank in this case I want I don't want to delete any orders if a customer's ever removed that's kind of bad design so in this case I'll just say armed Elite Models dot set underscore null and what this is going to do is anytime we remove this orders customer for some reason if we delete them this order will remain in the database just with a no value for customer now for product we're actually going to do the exact same thing and we're gonna put that there except for the reference to the parent is just gonna be product so now in order has a relationship with a customer and with a product in in order to make this work let's just go ahead and run that migration so python managed dot py make migrations so the migrations are made let's go ahead and actually migrate it with the database now and we'll just do migrate I don't know why I can't spell right now but Python managed py migrate okay so that just created the relationship and one more thing I want to do now is actually add in that tag that I talked about so we'll go ahead and run one more migration so to make things faster I'll actually go ahead and just steal from this customer model here and I'll place the tag just above the order and we'll just change this to tag and the only thing we'll keep is gonna be the name for the tag so this is gonna be something like outdoors or kitchen or sporting whatever we want that tag to be and to create that relationship in our order we're gonna create an attribute called tags and just do models dot many-to-many field and all we need to do is specify which model we want to create that relationship with so in this case it's going to be tag so let's go ahead and run one more migration so make migrations okay so the tag was created and let's migrate it so that was all complete and let's go ahead and just add that to the admin panel real quick throw in tag I'll put that above and let's get our server running and actually see what that looks like so I'll just drag it in here from the other screen and let's go to our admin panel okay so we have our tags products orders let's go ahead and actually create some customers and we'll show you how that relationship works and before we get too far I actually noticed quite a big issue that we just created tag is actually supposed to be to product and not to order so I'm gonna go ahead and just move this up here just above product and we'll actually take out tags from order and just copy that in add that to product so let's just go ahead and ream I gray our database and let's just run those migrations it removed that from one field and added it to another ream I grate that and now we can get back to that actually just caught that as I was thinking about which products I was gonna add so I'll just go ahead and run the server okay so now that we got that let's go ahead and add our customer so Peter Piper and let's just go ahead and add John Doe and let's give him a number we'll just use one of those and John let's just do John Doe Corp calm now and let's go ahead and add some tags so I'm gonna reference what we had in that diagram that I created so we'll do sports summer and kitchen okay so now that we have the tags let's go ahead and add the products so for products we'll have barbecue grill and this is where we'll actually be able to see that many to many relationships so we'll set a price really quick let's just do $200 category outdoor and I'm gonna just add in a dash for description because it's a required field right now and in order to add the tag as well later on actually change this to a check here a multi-select checkbox field but for now we need to hit control because if I just click on each one it's gonna only allow me to select one so if I hit control and press kitchen and summer we can say both of those so if I hit save and I look at the product we now have a reference to summer and kitchen so those are the two tags so let's go ahead and add in a few more products and just to fix this really quick it's actually bothering me I'll go ahead and add that string value so we'll do that for product and we'll just leave that to be the name and let's see if we have ordered properly and actually one more thing for the description null is for form submission I think so we also need to add blinked and set that to true so in this case we actually won't need that description there and that should do it actually just throw that a to order to okay and for order you know I'll leave that blank that's a whole nother issue okay so let's get back to it and finish up with the products so the second product we'll add this will be dishes and the price for dishes let's just set that to you $50 and that's going to be indoor and I don't think we need the description now so we'll just set that to kitchen there we go so we don't need that description now because we added blank and the last product is going to be ball so we'll set the price of that to let's say $30 outdoor and sports and summer okay so those are our products let's go ahead and just add in some actual orders now so orders and this is going to go over that mini to one relationship so orders we need to give it a customer so let's say Peter Piper wants he wants to buy a ball and we'll just set that to pending now Peter wants to place another order so Peter also wants to buy a grill so we'll put that to pending because nothing's happened and we'll just give one more order and we'll just set this to John Doe so this is the many to one relationship it gives us a drop-down menu unlike that Minnie - Minnie field that gave us that multi select option so John will buy some dishes and that will also be two pending so now we have tags products orders and customers in the database and in the next video we'll actually go over querying these from the database and actually pull in these items out and grabbing those related fields also and then the video after that and I might put them into one video but it'll be on rendering that data out into a template so we can actually see it within our app
Info
Channel: Dennis Ivy
Views: 127,803
Rating: 4.9599667 out of 5
Keywords: Programming, Software Developer, Dennis Ivy, Dennis Ivanov, Databases, Many to Many Relationships, One to Many Relationships, Many to One Relationships, Django, Python
Id: wIPHER2UBB4
Channel Id: undefined
Length: 13min 59sec (839 seconds)
Published: Tue Dec 03 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.