Setting up Alembic with SQLAlchemy
In this video, Jose Haro Peralta explains how to set up Alembic to manage migrations with SQLAlchemy. You'll also learn to add to or query data from the database using SQLAlchemy.
Chapters:
0:00 Introduction
2:54 Setting up the environment for the tutorial and installing dependencies
4:30 Initialising Alembic
5:13 Configuring Alembic
6:52 Creating a SQLAlchemy model
10:02 Creating a migration with Alembic
10:52 Running a migration with Alembic
11:15 Writing to the database using SQLAlchemy models
14:20 Reading from the database using SQLAlchemy models
14:54 Enriching SQLAlchemy models with additional propetries and methods
17:53 Wrapping up
What are SQLAlchemy and Alembic?
* SQLAlchemy is Python's most popular Object Relational Mapper (ORM). ORMs are frameworks that offer an object-oriented interface to your database tables. ORMs give you a layer of abstraction on top of SQL, so you don't have to write SQL queries by hand - instead, you just write code. ORMs also abstract away the differences between SQL engines, such as PostgreSQL and MySQL, so you can switch between one and the other without having to change your code. You can learn more about SQLAlchemy with its official documentation: www.sqlalchemy....
* Alembic is a database migrations management framework. Alembic ensures that your database schemas accurately reflect the data models that you define with SQLAlchemy. You can learn more about Alembic with its official documentation: alembic.sqlalc....
The code for this tutorial is available in GitHub: github.com/abu....
If you liked this video, please like it and share it with your network! You can also subscribe to my channel! All this goes a long way to supporting me to continue creating this kind of content.
I'm also the author of Microservice APIs with Examples in Python. It's a book dedicated to explaning how to design and build microservices and APIs, with examples in Python. If you're interested in the book, you can get a copy through this link: mng.bz/jy4x. Feel free to use the following code to get a 40% discount: slperalta.
You can also download two chapters of the book for free from this link: www.microapis....
ERRATA:
* In the section 7:20-7:40 I wrongly say that declarative_base() is an Alembic function. declarative_base() is a SQLALchemy function and the Base class is a SQLAlchemy class.
Пікірлер: 53
You don't come across resources like this very often. Thank you!!!
@microapis
2 ай бұрын
Thank you for your supportive comment @kingsleyakpan3828!
Very useful. Thanks for the guide! Its essential to get off on the right path with these complex setups. There is so much to stitch together these days that these tutorials are invaluable. Good job.
@microapis
9 ай бұрын
Thank you for your kind words 🚀! You're absolutely right 💯. I've seen a ton of bad practices around, which was the motivation to make these videos!
@petermoore8811
9 ай бұрын
@microapis I've been using alembic a while now, and it's great. But I think they should have numbered the migration files so it sorts in the file system. It might be an easy thing to add. Might do a PR with that function added
That is the best tutorial on youtube for sqlalchemy + alembic, thank you!
@microapis
2 жыл бұрын
Thank you so much for the nice feedback!!
Best Video out of All Alembic explanation because you have introduced SQLAlchemy rather doing Alembic way of creation of tables. Thanks a lot
@microapis
Жыл бұрын
Hi Vashu thank you so much for your kind feedback 😍!
Short, sharp and to the point. Thank you! Please keep making videos like this. I'd also encourage you to make a Udemy course version of your book. Keep up the great work.
@microapis
Жыл бұрын
Hi Roberto thank you for your kind feedback! I'm planning a Udemy course actually 😁, hopefully will release early next year!
Thank you very much, you help me a lot! Congrats for this video.
@microapis
2 ай бұрын
Thank you for your kind comment @rafaelfurquim745 🙌!
best tutorial for sqlalchemy + alembic
@microapis
2 жыл бұрын
Thank you so much for your kind feedback @Rashmi!
Thank you so much for this video. Super clear on how to use Alembic in combination with SQLAlchemy :)
@microapis
Жыл бұрын
Thank you for your kind feedback Lucas 🚀!
This video helped me to understand Alembic documentation, thank you man!
@microapis
2 жыл бұрын
Hi Daniel thank you so much for your kind feedback! I'm really glad to hear the video helped you with with the Alembic docs!
great intro to alembic, thanks
@microapis
2 жыл бұрын
Thank you for your kind feedback @Conor!
Very informative. Much gratitude.
@microapis
9 ай бұрын
Thank you for your kind feedback 🙌!
Great tutorial, thanks! 👍
@microapis
2 жыл бұрын
Thank you for your feedback!
Thanks bro, that was amazing,
@microapis
Жыл бұрын
Hi Doble thank you for your kind feedback 🙌🙏!
Thank you so much
Thank you very much man, great video. I would love if you had added some extra bits of information other than the introductory stuff but maybe in another video? Thanks again!
@microapis
Жыл бұрын
Thank you so much for your feedback! That's right, I'm planning another video with more advanced material about SQLAlchemy. I hope to be able to release it soon!
Great video, thank you. You can do f"{variable_name=}" to print variable_name=value
@microapis
7 ай бұрын
Thanks for the tip @davidcarter4454 I didn't know that one 👏!
Thanks a lot!
Nice video, in this video you created table....can you show the extension by adding a new column in already created table and show how upgrade and down grade can be done. Also please show how to support multi db
@microapis
Жыл бұрын
Hi Naveen thank you for your nice feedback and for your suggestions! I'm preparing work for a new video and I'll make sure I cover those topics.
What are you using to import things at 8:13? Is it a VSCode plugin?
@microapis
Жыл бұрын
Hi Daniel sorry for my late reply! Thank you for your question! I use Pycharm. There's an easy keyboard shortcut for this in Pycharm. Unfortunately, I don't have experience with VSCode so I can't advice, but I believe both IDEs are quite similar so I'm sure there must be a plugin or perhaps built-in support for this
Great Video!
@microapis
Жыл бұрын
Thank you for your kind feedback!
Very good!!
@microapis
9 ай бұрын
Thank you for your kind words ❤!
Thanks!
@microapis
2 жыл бұрын
Thanks for watching and for your kind feedback!
Thanks a lot! can you please make a video on creating replaceable objects such as views, materialized views, SPs etc.?
@microapis
Жыл бұрын
Hi Dr Kingston thanks for the suggestion, that's an awesome idea! It's really not very difficult and these are great performance boosters. I used views extensively in a recent project to speed up queries for a machine learning engine. I'll put this on the list of upcoming videos 🚀!
Hello, I've tried your code locally and when trying to autogenerate the alembic migration file I get this error: ModuleNotFoundError: No module named 'models' Can you please help?
@microapis
2 жыл бұрын
Hi thanks for checking out the tutorial and for trying out the code! Can I check first of all that your setup looks exactly like in the video (you can check the GitHub repository as well: github.com/abunuwas/alembic-sqlalchemy-tutorial)? In particular, to make sure your models file is named correctly without a typo. Second, make sure you're running the command from the root directory of the application (where models.py is located) as I do in the video. If you're using an IDE like PyCharm or VS Code to run the code, make sure you set PYTHONPATH to the root directory of the application. Please let me know if this helps. Feel free to provide more details if it still does't work. You can raise an Issue in the GitHub repo if that's easier.
@dughall_
2 жыл бұрын
@@microapis Hello, thank you for the answer, the issue was fixed eventually, my mistake was installing alembic from apt and not from pip.
@microapis
2 жыл бұрын
@@dughall_ Got it glad to hear you were able to fix it!
Hey there, amazing video! I would not have understood Alembic without it, I do have once question though. Would this be good for use in a open source project that anyone can use to help them migrate there database when the project updates or is there a better way?
@microapis
2 жыл бұрын
Thanks for your kind feedback! You think of a library that makes it easier to set up SQLAlchemy with Alembic? I think that could be useful yeah!
Hey is it compatibile with sqlalchemy 1.4, I'm too new to see the difference but I'd like to focus on the future! :)
@microapis
2 жыл бұрын
Hi @doremicocoparis thanks for the question! I did the video using SQLAlchemy version 1.4.29 so it should work the same for all versions of 1.4. It'll also work for previous versions such as 1.3.