Category: Database

2021-09-01T13:23:29Z

Optimizing Slow SQL Queries

Most database problems go unnoticed during development, because we tend to code our applications using small datasets. It is when the application has been on production for some time that database performance issues start to appear, causing parts of the application to become slower and slower as the database continues to grow.

How do you debug and identify this type of problems? In this article I'm going to show you how to fix the most common database performance problems, which are those that are caused by improper indexing. Examples for Postgres, MySQL and SQLite are included!

This article was voted by my supporters on Patreon. Would you like to support my work, and as a thank you be able to vote on my future articles and have access to a chat room where I hang out and answer questions? Become a Patron!

3 comments

2020-07-30T21:55:00Z

Fixing ALTER TABLE errors with Flask-Migrate and SQLite

If you've done any work with SQLite databases you surely know that this database is very limited in terms of making changes to the database schema. When working with a migration framework such as Flask-Migrate, it is common to end up with migration scripts that fail to upgrade or downgrade just because they need to remove or modify a column in a table, something that SQLite does not support.

In this article I'm going to discuss this limitation of the SQLite database, and show you a workaround that is specific to Flask-Migrate and Alembic.

16 comments

2020-04-09T17:03:54Z

How To Add Flask-Migrate To An Existing Project

A question that I frequently get is how to start tracking database migrations with my Flask-Migrate extension when the project has an established database, making it impossible to delete everything and start over from scratch. In this article and companion video I'm going to show you how to set up Flask-Migrate with zero risk for your current database.

62 comments

2019-10-14T15:24:42Z

Video: Four SQLAlchemy Tips

Posted by Miguel Grinberg under Database, Python, Video.

7 comments

2019-02-26T18:57:50Z

Nested Queries with SQLAlchemy ORM

Posted by Miguel Grinberg under Database, Python.

One of the most rewarding aspects of having a popular course online is that from time to time I get a question that forces me to learn something new. The other day a reader asked me how they can write a database query with an unusual ordering, and I had to stop for a minute (okay, it was more like half an hour) to think about how to do it within the context of a Flask and Flask-SQLAlchemy application. Are you ready to see some advanced SQLAlchemy action?

23 comments

2018-03-20T18:20:43Z

The Flask Mega-Tutorial Part XVI: Full-Text Search

This is the sixteenth installment of the Flask Mega-Tutorial series, in which I'm going to add a full-text search capability to Microblog.

356 comments

2018-01-30T18:07:32Z

The Flask Mega-Tutorial Part IX: Pagination

This is the ninth installment of the Flask Mega-Tutorial series, in which I'm going to tell you how to paginate lists of database entries.

128 comments

2018-01-23T20:01:33Z

The Flask Mega-Tutorial Part VIII: Followers

This is the eighth installment of the Flask Mega-Tutorial series, in which I'm going to tell you how to implement a "followers" feature similar to that of Twitter and other social networks.

335 comments

2017-12-26T19:34:07Z

The Flask Mega-Tutorial Part IV: Database

This is the fourth installment of the Flask Mega-Tutorial series, in which I'm going to tell you how to work with databases.

797 comments

2017-10-10T15:22:43Z

Implementing User Comments with SQLAlchemy

One of the most basic ways to keep the users of your web application engaged is to give them a space to write comments. These days, there are third-party services pretty much for everything, and comments is not an exception. Disqus and Facebook are popular services that allow you to embed comments into your site.

But what if you don't want to use an external service? In this article, I'm going to show you how I implement comments in Python, using the SQLAlchemy ORM and any of the database engines it supports. I'm going to start with an extremely simple approach, and then will go on to discuss a few advanced implementations with support for multiple levels of replies.

54 comments