Database migrations in FastAPI | Autogenerated migrations in FastAPI | Alembic migrations

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
yeah so we have we have created our table over there we have everything so for like uh uh let's do one thing let's create a separate folder for the models and put this model employee model into that particular table okay particular folder okay models we have one model stable and let's copy this thing and paste it into this model let's create one file called in employee I spaced it everything this copy is saved and yes remove this thing because we already have those thing into this model file and let's import this this employee mod model class into this main file how can we do that from models Port from orders import we have employees employee and now we have to binding this with the engine and create all for creating all them all the tables into database how can we do that employer dot base dot metadata we are taking create to your EIT create all way to bind with engine engine okay yes so we are done like we have import our model and adding this line this line basically will create double clear table and two database into database okay let's do one thing and now yeah now go for the alambic thing let's save it and go to the here this is like this is where we have we have everything for file folders here now we have to run the migration thing so for running migration first of all we have to initialize the alembic folder and do some modification into that so for that how can we do that we have command called alembic in it alembic okay so you can see some files already created into the particular uh directory so you can see like we have we got these folder this this versions will keep all the versions of your uh migrations and we got one file called alembic.ini that is the initial initialization file for this Olympic and we have to we have to add some we have to make some modification into that so what is the modification just go to the line number 63 and replace this URL with your URI so where we have URI we have URL into the DB setup this table let's copy it and just copy it over it is yeah just copy it and don't copy the codes over there just copy the line and just paste it to the alembic file move it and paste it okay and you are done you just add this thing into your file you just connected your alembic with your migrations now we have what you have to do you have to just run the migrations so how can we run the migration let me see whether I save the file all the files are saved okay how about how we can run the migration so further we have command called alembic Olympic and or Olympic revision Auto generate generate spelling should be okay oh we got some error code no module name cycle pg2 okay so we missed that module so nobody we need to install this modules yeah how can we do that so for that we we need to just type uh again pep install and psycho PG we can go for the cyclopedic binary and and or the other version as well so let's go for the psycho PG binary and the cyclopedia Pui psychop pg2 let's go with the binary okay you can pass you can take any version I'm going with the you can also pass like a version name like this so like this like I'm going with the 2.9.5 let's run it okay so you can see like a psycho B2 binary has been installed and what we can do next we can run this Auto generate again and we fear something we got some error over there let me see what is there okay so can't proceed with the auto generate option environmental script does not provide a metadata object of sequence object in the context okay so we got some error like let me check what what went wrong oh I guess I got the solution because we are using the engine over there but we have not import this engine so how can we do that simply from like a DB setup file board engine hopefully it will work now okay again we got somewhere like okay what is that error okay again we go somewhere let me fix it yeah I got the solution for that so the solution is that like we have we have ADD everything into our main file like we have import we have already targeting these military data but we have not configured this metadata with uh as the error mentioned with the dot Envy file inside the alembic folder so how can we do that let's go to the just open this alembic folder go to the env.pi file and and here you have to import your models files okay how can we do that so for that yeah simply you can you can import your file inside this folder so how can you do that so yeah from model import what what is the model name employee and we also need to import like a base model so from DB setup and import base over there and the target metadata should be base Dot metadata so we have imported our model we have imported our Base Class so these two changes you need to make and the resting will be okay I guess let me try again we have successfully generated the first version of the migration and if you go into the version section so now we have one version we have one version you can see like these column and everything is created so next thing we have to migrate everything into the post gear DB you can see like we don't have we have just we just have Olympic version but we don't have any plot table now in a relation thing how can we do that we have to Simply migrate our version so for that Olympic upgrade hat this is a command yeah so yeah so it has been my idea successfully and you can see like we got employee details as a table so finally we got the table if you want to see the content of inside this table so you can simply type select asterisk from employ details you can see we have three columns employee ID employee name and imply contact number all the three things that we created inside or employee model you can see and if I add anything into this file again like employee email it will be type column sorry and column worker 30 and so yeah we added one more column so if we want to migrate those changes into this MySQL table so we have to rerun the migration how can we rerun the migration First Command is alembly revision Auto generate and next thing is like alembic my battery is going Olympic revision that's not a revision alembic upgrade now go to here search it you can go you got imply email as well so that's how you update your model file into python site random migration and it will auto automatically update your personal database so I guess I'm running out my battery so yeah this is all about the migration how can we run the migration into the into the purse into the postgres and with the fast API so we use two modules SQL Alchemy and alembic lmb foreign easy SQL operation so we will see in the next video and what I will do I will paste all the code that I have written in this this directory into the GitHub and provide the link to the description so you guys can take the access and directly take access and do the required thing for your work okay so yeah thank you so much for watching this video have a great day bye
Info
Channel: Kavyansh Pandey
Views: 5,166
Rating: undefined out of 5
Keywords:
Id: tQ3pmkN6FnU
Channel Id: undefined
Length: 10min 26sec (626 seconds)
Published: Mon Mar 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.