2019-10-14T15:24:42Z
2019-02-26T18:57:50Z
Nested Queries with SQLAlchemy ORM
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?
16 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.
193 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.
82 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.
203 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.
532 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.
41 comments
2016-09-29T14:54:27Z
Implementing the "Soft Delete" Pattern with Flask and SQLAlchemy

Every time I find myself writing code to delete data from a database I get nervous. What if I later determine that I needed this piece of information, after all? For example, what if having access to this data that was deleted would have helped me reproduce or debug an issue? Or what if the data can be useful for audit purposes in a future version of the application?
You can find lots of reasons to never delete records from your database. But obviously these records that you saved from permanent deletion need to be marked as being "less interesting" than the rest, so that you have something you can use to filter them out in queries. The Soft Delete pattern is one of the available options to implement deletions without actually deleting the data. It does it by adding an extra column to your database table(s) that keeps track of the deleted state of each of its rows. This sounds straightforward to implement, and strictly speaking it is, but the complications that derive from the use of soft deletes are far from trivial. In this article I will discuss some of these issues and how I avoid them in Flask and SQLAlchemy based applications.
20 comments
2016-02-10T15:21:43Z
Resolving Database Schema Conflicts
If you work on a project that uses database migrations with other developers, it is likely that you have experienced migration conflicts at some point. These occur when two or more developers are merging unrelated features to the master source control branch at around the same time, with each feature requiring different changes to the database.
In this article, I'm going to describe the problem and its solution in detail, using an actual example based on my Flask-Migrate extension. While I will be using commands that are specific to Flask-Migrate and Alembic, the solution to the problem that I present here can be adapted to other database migration frameworks.
11 comments
2012-11-04T08:00:41Z
The Flask Mega-Tutorial, Part X: Full Text Search
(Great news! There is a new version of this tutorial!)
This is the tenth article in the series in which I document my experience writing web applications in Python using the Flask microframework.