Sequelize Migrations and Associations **New Version has been Released**

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey everyone in this walkthrough I'm going to cover a couple of topics migrations and associations so these two things can be pretty confusing and just hard to grasp at first until you do them a few times sometimes it takes numerous times of messing with them coding an application for it to all click it did for me and it does for most people so don't feel bad if they're pretty confusing at first but we're gonna walk through the basics of these two things and see equalise today just to show you the power of sequel eyes and how much you know it saves you in the writing of code and the time spent doing more complex things like these like joins and and stuff like that so I have an empty project here nothing in there so I'm going to go ahead and initialize it then I'm going to install some packages so I'm going to install Express sequel eyes my sequel to body parser and Express handlebars so we'll be using handlebars as well and this I'll go ahead and get those installed and now that I have sequel eyes the package installed in my project I can use the CLI so the CLI is a great tool that we use to speed up the process of creating all our models or migrations and also seeding our database and stuff like that we won't go into that today but maybe you later on I'll create a walkthrough for that as well so if you haven't installed the CLI yet you would do NPM install sequel eyes - CLI - JEE and that will install it globally to your system so you can use it in any project folder and once you do then you can run sequel eyes and since I have the package installed it gives me a list of all the commands I can run if you don't have the package install you'll see an error so just be aware of that and this list of commands gives us all the options and everything we need to get up and running one of the first commands have been a run is this an it command so out of the box if you just run an it by itself it gives you all of the folders and set you up with kind of a scaffold or a boilerplate kind of code to start you up with so I'll go ahead and run that sequel eyes in it if we look at the project now you'll see I have all these folders the config what I'm sure you probably have all seen before so this config file is how we connect to our server or my sequel server the cool thing about that it this is though is you have these different objects for different environments so for instance if you're on Heroku or another platform you're hosting your application on you can use this one for the production environment and such you set your connection settings up here for that and then you have your own local like on your on your computer developing all those connections would go up here so it's really nice to be able to have all those different connection settings I've created a database called sequel eyes migrations which I will use today for this example I'm gonna come over here to sequel probe and go ahead and just delete all the data I've already made playing around and we're gonna start fresh with nothing I'm also going to go to the - Google over here to Chrome and I'm going to type and sequal eyes and just go to the docks link here and what I'm looking for is this right here this option that we pass into our connection and this will keep the deprecation warnings from happening in the terminal because I don't like to see those so this will remove those for us I'm gonna go ahead and just wrap that in double quotes save and we're good to go with our connection file since my username is root and I don't have a password so the default works for me okay so folders we have we have the migrations folder we have a models folder with an index file in it if you're not aware of this this index file is kind of how we just get all of our models from any file so this I'll walk through it very briefly this file we start with an empty object here it will loop through and import basically all of the files or pull all the files in in your models folder other than the index so right here it's kind of sipping over the index file but looping through every other file and for each file it's going to import that whatever it's exporting in that case it's going to be a function write our models are going to be like a function that we're exporting it's going to import that function it's going to call it it's going to pass it a couple of things it's going to create a model and sequel eyes and then it's going to attach that model to this object so for every model you create every model file you have it's going to create a property representing that model in this object and then at the very bottom we just export that object so if you import this file you have access to every single one of your objects and it just makes it easier to you know deal with and work with your all your models alright so then we have the cedars folder which like I said before I'm not going to go into Cedars today but we can go over that at a later time that's just a bit way of like inserting dummy data into your database for testing and stuff like that so a lot of times you need a lot of data to really figure out how your application works with a lot of data so when you don't want to do that manually you have the you know you can use a bunch of tools to create some dummy data for you and the cedars are used for that okay so moving forward now that we have the anit done we can start with our migrations but the first thing we need to do is we need to create a model so from a model the CLI is going to create a migration as well and it's really easy to create models in the C in the terminal with the CLI we just run sequel eyes and then model colon and create that's the command to create a model and then you need to pass it some options so these options are required these two options are required the first one is name so we do - - name and then the name of the model so I'm gonna call it shop in my example today I'm going to represent like a coffee shop so the coffee shop is going to have coffees so we'll have two different models one for the shop and one for the coffees and then when we get into the associations and relationships the coffee's are going to belong to the shop so the shop will have a bunch of coffees and that's going to be the how those two models tie together but right now we're just going to work on the basic migration and getting that up and running so I need to continue here with the second option which is the attributes option and this is the these are the fields that you pass in that's your actual item in the database it's going to have so I'm just gonna have one field for the shop and that's gonna be a name and it's going to be a string so you just type in whatever name the fields gonna be and then a colon and then the type of that field so that's how easy it is I'm gonna hit enter and you'll see it created the model and if I go back we now have a new model file in our models folder and I like to go in and just rename this for some reason there's probably an option for this but I haven't really researched it it lowercases all of your model file names and I like to have them capitalized so it's not a big deal I just go in and capitalize the model names and if you look at our model you'll see it's the basic function type so we're exporting a function that function now this is this of course is an arrow function at es6 so just be aware of that if you are not up on your es6 yet i've covered es6 and before i believe with you but a good thing to do would be to go to youtube or sources resources like that and find some you know videos on es6 there's plenty of them out there to get up to speed on these arrow functions but moving on we are receiving the sequel eyes object and the datatypes object which is how you set your type of course this has all been created for us already the variable shop you know the model and then it defines the model name so here this is important if you're not using migration so just remember if you're using the sync method which what I'm saying sync I mean this method that sequel eyes uses to automatically create your tables it will read you know as long as you sync your model it will automatically read your fields and create a table for you based on your schema you set up here right but since we're using migrations we don't really need to worry about it so I will leave this as is just a capital shop for my model name I'm just remember this is this is the actual name that you're setting here not your variable that has nothing to do with it's this name and the string here and then this associate method here we won't worry about it right now that's wasabi actually set up our associations will do that in a second and then it just returns the model file which in turn is pulled in in the index here and a model is created from that and then attached to the outgoing object which we can import anywhere and use any of our models so pretty awesome kind of methodology there and paradigm they're using for this and if we go into our migrations there's a file that's been created so this is automatically created when you make a model and here it's just an exported object and it has two methods and these two method names up and down are common across the board in multiple programming languages so if you've ever dealt with another language you've might have seen this before if you ever have like seen migrations or read anything about migrations or watched a video there's this concept called up-and-down right we up a table and we destroy it running the down right so this is the this is what's going to happen a migration is simply an object that has some methods on it that will create a table and then or do some other things like update a column or something like that but in this case it's gonna be just a basic create a table and then if we need to we can also run this to destroy the table so to get rid of it and that's for you know like when you if you might you might add some fields to your table or remove a field or update a field or something like that and you need to you know basically just remove that table and then pop it back into the database again that's why this method is very important for that so we can quickly just remove it and then add it back again okay so getting into our fields you'll notice that my shop model only has the name attribute are the name field right that I set when I created the model but uh sequel eyes has made some other fields for us automatically the ID field which we need the created app these are kind of optional right but they're really important when it comes to you know organizing and dealing with your data Leduc later on you can pull the win an item much was actually created in the database and every time that atom is updated at any time this field will be updated and you'll also be able to have that time and day when that happened which is pretty cool so here's our table name and so this query interface object that's been you know pulled in from the function here or sent through it's calling this create table and here's the name of our table so in this case it's a capital shops right so how does it get this name like if you ever wondered if you look at the model name the model name is shop capital shop so sequel eyes will look at this name and create the table name based on that model name and the normal convention is it's going to assume the plural version of this name so in this case that's right sometimes it's a little bit off most of the time it's pretty smart and gets the plural you know version right so shops is going to be than any other our table but I don't like it to be capitalized so the another you know normal convention is for the table to be lowercase so I would just change that and down here also we need to make sure this one is lowercase and at this point we're ready to run our migration so running a migration just may basically means we're going to tell sequel eyes to read this file and then run the up method and it's going to create a table based on these fields and that's all it's basically going to do at this point right so let's run it so let's do sequel eyes and then you do DB : and then migrate and just hit enter and there we go so now if we look at our database if I go back to sequel pro and refresh we now have this shops table so just be aware that this equalised meta table will be created as well and it's just a way of tracking all of the migrations that you have made so sequel eyes uses this table for that I'll go ahead and delete those two and should be good to go now if at any time you needed to like remove that table that you made and you know make a change to it and remove it all you have to do is run sequel eyes DB migrate colon undo to do the last migration or colon all at the end of that to do undo all the migrations you've ran so now if I run that and go back and refresh you'll see that we lost our shop shops table it's gone and that's how it works so it's simply just removes it but we're gonna keep that so we're gonna go ahead and run it again so our table has been traded again and now we should be good to go with our migrations right that's the basics of migrations so let's move on to associations and doing so before we start let's go ahead and create our coffee model so remember I said we're gonna have coffee shop and the shop is gonna have coffees so we need a model for that let's go back and run sequel eyes model create the name of it's gonna be coffee and our fields are gonna be name string and I'm gonna have type which is also a string and that's gonna be like the type of roast you know like dark roast medium roast light roasts that kind of deal so that's just my two fields that I made for this coffee we're keeping it really simple today if we come back we now have our coffee model of course it's lowercase so I'm just going to go in read that with the capital C and if we look we have our two fields that have been created and then we go into our migration file and we will change this I'll just select both of those and just change the capital C to a lowercase but there's one thing we need yes we have all the the basic fields made for us but there's one feel that it didn't make that we're gonna need because we're gonna be creating an association so this model is gonna be related to the shop model or this table is gonna be related to the shop table so think of it like this way shop is gonna be the parent and it's gonna have children which are coffees right so there has to be a some way of associating a shop with its coffees so every time we create a coffee it's gonna be related to one particular shop and to do that we just basically need to attach the ID of that particular shop to our coffee each time a coffee is made so we're gonna have you know a whole bunch of shops but when we make a coffee it's got to be attached to that particular shop and we can just simply use the ID of the shop on our coffee item when it's made to do that that make that really easy Association and that's called a foreign key so where foreign key is a field that's an ID like a number associated with an ID from another table so that's what we're going to be doing here we're going to attach the shop ID number to the coffee so I'm gonna make a field here called shop ID and it's going to be C tight C lies dot and it's gonna be an integer because our IDs are all integers right so that's why I'm setting this to an integer as well okay so now that we have this field we can run the migration because we have our foreign key setup so every time you make a new coffee it's going to have the shop associated with it that that ID I'm also added to the item alright so let's go back and run this migration simply by running sequel eyes DB migrate and it's only gonna run the migrations that haven't been run yet right so the other one has been run already so now if i refresh we have both of our tables and if you look we have that extra field their shop ID that we're gonna need for the Association so now let's go into our model files so in the shop file it's gonna work like this it's gonna be like this it's gonna be the shop has many coffees right now there's a method we're gonna be running and the method if you didn't guess already is called has many this is the this is the type of Association that we're making because the shop is going to have many coffees right and that's going to be like a child of shop so simply all you have to do is run shop has many but now we need to pass the coffee model in so you might think we should import the coffee model in this file and then we could pass it into here but you don't actually have to do that because secret lies makes it easy by calling this function and it passes you all of your models that you have in this object right here so all I have to do is just do models dot and then coffee and I've just passed the model into this method okay so that's the that's the best relationship as far as the shop goes right because it's gonna have a bunch of coffees but now I'm going to go into coffee here and we need to set up the association here so and that's going to be coffee belongs to a shop so whenever a new coffee is made it's going to belong to a particular shop and we do that by running coffee dot B Long's to models dot shop so now that we've set up our associations it's really as easy as that and if you if you were wondering I'm gonna save this now we can run some tests on this just to see how it works but the first thing I need to do is go ahead and set up a server file so let's create a server jas file and we're going to I'm gonna use es6 in this instance today for a little you know some stuff most of this stuff and we're really going to be working mainly in this file the server file with all of our routes and everything I'm just going to keep it pretty basic and simple today but we're gonna use handlebars as well so we'll incorporate coorporate that in a minute I'm gonna go ahead and pull in Express body parser and we'll pull in path and I'm going to set up my or go ahead and instantiate my Express application so I have new Express app instance and then let's go over here to Google let's go to body parser and click on the package link scroll down to the first block of code and grab two lines that have the comments on them and then I'm going to change this to true and so body parser is set up and then I'm going gonna go to express handlebars go to that package link scroll down and grab these two lines and then I need a I need to import it up here so I'm just gonna do Const just paste that and require Express hand waters alright so now I express it handlebars are set up but since we're declaring that we're going to be using a default layout file we need to go ahead and set that up so I'm gonna make a folder called views and inside of that we need a folder called layouts and then I file called main dot handlebars the main dot handlebars is important because we're saying right here that our layout file is called main right and that lies within views which is required and then layouts which is required and then main dot handlebars and in here I'm just gonna go ahead and do our basic markup and we'll do a coffee shop a purser be able to do our triple handlebars or curly braces and output our a template file code there and then I'm gonna go ahead and just run app dot listen I'm not going to create a variable to store the port today I'm just gonna pass in 5,000 I'm gonna use an arrow function here to one mind this and just say listening on fourth five thousand and then right here you know we're gonna put all of our routes in in a second but for now I'm gonna play around with my models so easy way to import them it's just to say coffee shop equals require and then we say dot slash models and then now that I'm pulling in the folder nodejs by default if there's an index file inside the folder when you try to import the folder it's gonna look for that file and if it finds it that's what it's going to import so what are we importing once again we're importing this object right here that contains all of our models so all I have to do on this now since this is an object is grab the shop property from that object so dot shop will copy this and I will say coffee so now we have our two models imported so let's try something let's say shop create and to create a shop we need just a name right so I'm gonna say Starbucks then we have the shop right so es6 if you only have one argument being passed in one parameter you don't need the curly of the parentheses for the parameter and you can just leave those off and it still works just the same way and what we're going to do in here though is I'm going to go ahead and create a coffee that's associated with this shop so now that we've made the Association here in our coffee and in our shop because of that the shop instance there this shop object that's been created right here which if you didn't know what's happening when we create that new shop item it's going to be passed right here to the then call back and every single one of these items now will have a method attached to them are a few methods actually and these methods are created through the association's one of them is create coffee it's you're probably wondering that's that's weird but yes it looks at the model name of the associated model and it creates a method that will attach the Associated ID of the shop to that new coffee item for you automatically without having to create a coffee and do it manually this method will do all that for you so in here since we're creating a coffee at this time we're going to be passing in the name of the coffee I'll say like Columbian and then the type is going to be like dark rose so I'll just say dark and then we will do just a call back and say console.log worked and if that's all good we should be able to create the item so I'm gonna run node server j/s and there we go it says worked so if i come back to our database and refresh our shops we have our Starbucks item added and if I go into coffees you'll see that I have the Columbian Ida and the shop ID is one right because the ID of Starbucks is one so that's the association anytime I create a coffee for Starbucks it's going to have the shop ID of one right here attached okay so now we're good with the relationship we know that it's working we ran a little test here to see if it was so I'm just gonna comment this out and I'm gonna give myself a little room down here let's start creating our routes so how I'm gonna work this is when I have like a main the main route route and it's just going to load a view which is gonna show our coffees or show our shops and it's gonna have a forum that's going to let us like you know enter a shop a new shop and also each one of the shops is gonna have like a little form to enter in a coffee so let's go ahead and set up our route route so slash and I'm using the arrow functions as well here here we're going to get all the shots so I'm gonna do find all excuse me there's one important property here and this is include so the include property lets us pull the association's in when we're finding all of an item we can also find all the associated like child items or whatever with this particular model right so since I want to get all the coffees of this shop I just use the include property and then pass it an array and then that array is going to contain all of the associated models so since our coffee is associated with the shop model we are going to pass that model in so if you don't remember we have it right here so that's what I'm passing in right here so since we did that we're gonna get something cool with that so if I just call this before we mess with the actual view let's go up here and let me show you how that works so right below here I'm going to do shop find all and we're going to do include pass an array coffee then shops and let's console the shops that we get back so since we've saved one now we should be able to run this and we see that we have an array in the first object here ID of one Starbucks right but check this out we have this coffees property and it's an array so this property contains all of the associated coffees with Starbucks that's pretty awesome huh so if I grab that first object out of the shops array and let's just console.log coffees run us again and there we go we get an array of all the coffees so we only have one right now Columbian but you see how easy this is to get all the associated items with the shop really awesome I'm going to go ahead and run no time on its going to I didn't set up the file so let's run node Mon server je s there we go so now I don't have to keep going back in canceling and restarting the server I'll let no daman do that for me I'm going to comment this out as well so now that we know that what that does we're going to at this point res dot render our template and we're gonna render the index template which we haven't created yet I'm gonna pass it a property of shops and that's going to be set to our shops array that we get back from the database right here so let me go ahead in our views folder create an index not handlebars file just so we have that and well I guess let's go ahead and create another route so we have our view route pretty much set up here we also need a route to actually create a new shop so this is gonna be a post route of course so from the front and there's gonna be a form that's gonna send some information through and our routes gonna be shop so when the form submits is gonna go to slash shops we're gonna get the request and response and it's really easy to create a shop correct because all we have is a name property but we can make it even easier by simply just passing in the request dot body here it's a request that body as everybody's should be fully aware by now is the object of information sent through from the form and that form object is going to have the name property and whatever they typed in as the name so all we have to do is just pass that object in so then we're gonna get we're gonna just go ahead and call this callback run this callback and then just redirect them to the home page again so as I mentioned before redirect simply just calls another method that you set up or another route that you set up so this route is gonna be called again it's gonna load all the shops and then re render the index okay so that's that's the simple add a shop route we also need a route to add a new coffee so another post route and this path the URL is going to be a little different so we're gonna we're gonna send the information to slash coffee but I'm also going to add another end of this URL we're gonna use like a placeholder so placeholders and Express are you are created using the colon and this is a query parameter or a parameter basically and it's dynamic so whatever number when they create a new coffee when let me submit the form from the front-end there's gonna be a number that's gonna be sent through maybe like one or two based on the ID of the shop so we want to get the ID of the shop now I can just say like any name I want so let's do shop underscore ID how about that so now our our parameter name is gonna be shop ID and we can use that inside the actual route when we're setting it up so here I will do request response and then here we're going to create a new coffee so I'm going to run coffee create and we're going to pass in the request body so request body's going to contain the name of the coffee and the type of the coffee from the form when they fill it out so but I also want to add my own extra property here so we're not going to do that we're going to use something es6 called the spread operator so if I do dot dot dot request dot body it's going to grab the request the body object and it's gonna pass all the properties from that body object into this empty object so the name and the type will be passed in and then right after this I can comma and add another property of my own which is going to be shot ID because we or we have to create that shop ID to associate this coffee with its shop and this is gonna be the request dot params in this case because how do we access these dynamic values that were setting up here so I want to get the number that they sent through for the shop but I couldn't just type in me Yuli one or two or three because I don't know which number is going to be sent through right so this allows me to dynamically grab that number right from the forum are from the actual requests when they send it through and we grab this with the params object and then the name of our dynamic query right so we do shop underscore ID okay so then we're going to do then and we're gonna do response stop redirect again back to the home page so once again we'll just redirect back to this route and load the home page so those are our our routes really simple few routes here just to create a shop create a coffee and associate that coffee with its shop okay so now let's go into our index template so I'm gonna close out a few files here let's go into our index handlebars template and here we're gonna start setting up our HTML okay so not much HTML but just a few things to really basic try to keep it as basic as possible here to show you an example I'm gonna have a header up top and this is gonna be right above our form to create a new shop so we're gonna have a class called shop - form we're also gonna have a class column so these two classes in my CSS I'm gonna I've made some I've pre-made some CSS for this I won't be going through that today but feel free to play with it and you know just check out my CSS there's nothing too crazy but I just wanted to make it look a little better than just straight HTML in the browser cuz that would be pretty terrible so our action is the route the route that we're sending this through right so sending this information too and that route of course is going to be slash shops correct so here I'm gonna say slash shops and then our method is going to be post and then for our input it's just simply going to be a text input and our related property right is going to be called name so this is going to be the name of the shop so the name attribute esporte is important because the back end will be looking for this right to associate what you type into this input with this property right here so when we send it through back ends gonna have that name property which is gonna be attached to your request dot body correct okay so I'm gonna have a placeholder as well and this is going to be just telling the user what to type into this box so a shop name and then a basic submit button okay and then right below this I'm gonna have another header and this one's going to be the the header for the shop listing right so I'm gonna say shop header for the class and then shop listing and then we're going to loop through all of our shops that we get that we're sending through remember and the route route here I'm sending through these shops property which is the template will receive and so I want to loop through every shop that we get here but first I'm going to wrap all of those shop divs in a row so I'm gonna create a row using flexbox so I'm using a couple of flexbox things today in this example feel free to check it out because flexbox is really powerful we have you know CSS grid now to all these great tools for layout in the browser it's just amazing the power of CSS and 2018 okay so now I want to go ahead and start my loop so we're gonna use double curly braces and then hash tag and then each so this is our handlebars each loop so for each shop in the shops array so the name of our property we're sending through of course right is shops so I place that in there and then down here I'm going to end my each loop then I'm going to create a div with a class of shop and a class of column for each shop object in this array I mean here I'm going to have an h2 and this will just contain the name of the shop so remember we can access the properties of the current object in the loop right so this is an array of objects the current object in the loop you can either access it long version with this dot name so I'm getting the name property from that object or the short version is just to take out this and just declare the actual property name okay so then we're gonna have a form nested in here and this is the form that's gonna submit the new coffee so when we add a new coffee we're going to be sending this to what if you look at our route we set up we're gonna send this to slash coffee and then I need the ID number right so how do we get that well let's check it out I do slash coffee slash we can easily get the ID of the current shop object in the loop just by simply outputting the ID so I'm gonna just do the double quote curly braces here and just say ID and that will pull the current ID number of the current object in the loop as simple as that so here's the real number the actual number we're outputting here that's what we're submitting to and here this will dynamically grab that number one two three whatever it is and here I can get that number by using this dynamic query that I set up here really cool stuff and then the C method is going to be post we're also gonna add a class of column to this and then it's gonna be two inputs right we have the name of the coffee and the type of coffee so the first one's gonna be named place holder and then coffee name second one's going to be the type of coffee and then just a simple button that's gonna say add coffee okay so there's our form so then we're gonna have an H three right below that and this is going to be the listing of all the coffees for this particular shop so now I need another each loop and this is gonna be a nested each loop so when you nest in each loop inside of another each loop in handlebars you can access properties from the current object in the parent loop so since this is our shops array right each one of those shops each one of those shop objects has a property called coffees right remember that that we get that property coffees and it has all the associated coffees with that shop I can easily loop through that property just by stating that property in this nested each loop so if I do this this is referring to shops are the current object in shops dot coffees so really easy and makes it super quick to grab nested objects or arrays from your parent object in the loop okay so here I'm gonna for each coffee we're gonna click create a class our div with a class of coffee this is gonna have an H for with a name of the coffee I'm in a paragraph then we're going to output the type so dark medium you know light I'm gonna say roast right after that sort of you like it'll output like dark roast or medium roast and we can also do something in handlebars to accommodate or to allow for empty value so if we don't have any coffees for the shop currently then we can do something here to output some other HTML in the case that you know no coffees have been saved yet so I'm gonna do a simple else and then right below here we can write any HTML that's going to be outputted if coffees is an empty array and has no values in it so I'm going to do an h4 and I'm gonna let's see h4 and this is gonna have a class of empty and I'm just gonna say no coffees let's have been added like that and I believe that's it that's all the HTML we need I'm also going to go back to our server j/s and right here I'm going to say app dot use and then Express dot static and then path join underscore underscore der name and then point to a public folder with inside of our sequel eyes migrations example folder right so remember this is the way to set up your front in files right any files that you want the browser to have access to out of the box because remember all these files are blocked the front end does not have access to them unless we specifically do give them access or send them the file or something like that so I'm gonna create a folder called public and now that we have added this line to our server file any file or folder inside a public is now accessible on the front end browser we're gonna go ahead and create a folder in there called CSS and then a file called style dot CSS and in this case like I said I'm not going to go into the CSS that I made for this of course feel free to check it out I'm just going to copy over and hoops paste that in so here's all the CSS and just a quick little summarization I made in doe did some basic resets made some global classes you saw the ones that I used right row and column wrap is just to make sure that the row wraps if the items can't fit in the row unless all flexbox stuff and then down here we just have some custom classes that I made for each one of the items and not much not not many advanced things going on here so pretty basic CSS just to make things look a little better okay so now getting back to our server file there's one thing that I did not do in the coffee relationship here that I ran we can pass a second argument into this and it's going to be an object so these are options optional optional options that you can pass in if you need to right and the foreign key is is the important thing here so when I say foreign key I'm referring to shop ID right and it's camel case right now so lower case and then a capital I and D right so that's camel case by default though sequel eyes will be looking for a Pascal case and Pascal case is simply the first letter is capitalized and then every word after that is capitalized so I don't want it to be the default Pascal case I want it to actually be the camel case so I'm gonna specify that shop ID should be camel case so I'm gonna say for and key like this and then we can actually designate what that foreign key is going to be so I'm gonna just set it to the shop ID camel case that I want it to be like this because like I said the default is like this but we're gonna set it to that and now we should be all good to go if we come back let's just make sure there's no errors and if I go to localhost 5,000 oh I did not load my CSS file so let's do that let's go to layouts and main and pull in that CSS file so CSS slash style dot CSS now if i refresh there we go okay so you see it's really basic but check it out we have a little form up here to add a new shop and then our shop listing and then this is going to list out all of our shops right and you see we have already created one so we see the little form here to create a new coffee and then our coffee listing for that shop and it shows our one coffee that we've added let me go ahead and try to add a new coffee so I'm gonna say Italian and then this is a dark roast as well usually if I hit enter there it is that send us back to the index template and we see our new coffee that we just added awesome okay so that works let's add a new shop so I'm gonna say Dunkin Donuts there we go we get a new Dunkin Donuts shop if you notice we don't have the same coffees right no coffees have been added to this particular shop because this is a different item in the table so if you look the idea of one right in Dunkin Donuts is two and these two coffees are related to the shop with an ID of one so that's why this is not showing any coffees rights but if I add one let's add Columbian to Dunkin Donuts hit enter there we go now Dunkin Donuts has the Columbian coffee as well so this is the application and I know it's you know it's gonna take some time to go through it and work with it and play with associations and migrations to to figure them out but but that's the that's the key you have to code you have to try it and you're gonna face you know bugs and errors and all these different things but in and trying it and actually writing code and you know pushing to actually make the application work you'll work through all those bugs and hopefully this example what gets you going and get you started on dealing with migrations and associations all right thanks everybody and we will talk to you later on
Info
Channel: JD Tadlock
Views: 33,295
Rating: undefined out of 5
Keywords: sequelize, mysql, sql, express, node, associations, migrations
Id: 9xJLcTxlEIs
Channel Id: undefined
Length: 56min 23sec (3383 seconds)
Published: Wed Mar 28 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.