Tech I love: SQLAlchemy


My goal with this blog entry is to transition my “tech I love” series into an architectural series focused around data modeling. Data modeling is one of the most common problems software development teams encounter.  Fortunately, any language you might want to use for higher-level contemporary development will be equipped with one or more ORMs which make handling data models very approachable. In Python, the leader is SQLAlchemy (SA).

SQLAlchemy provides a fantastic method for building data models. It allows the developer to define entities in a concise manner. Those entities can then be used for just about anything one can imagine. Automatically generate new database tables to represent your entities! Transparently load and save database data as your entities change in working memory! All the good stuff you would expect from an ORM.

SA starts looking even better when you consider some of the less obvious benefits. For example, consider the case of wanting to change over from mysql to postgres. Should your interactions with the database be performed in raw sql, you would have quite an effort ahead of you. It would necessitate rewriting most, or all, of those statements. With SA, you’ll probably get away with simply changing the connection string! SA confers all the benefits you would expect from taking a major facet of development and abstracting it away into a compartmentalized toolkit.

I’ve been using SA for so long at this point that it is a struggle to imagine how life was like “before”. If you’re using an alternative Python ORM, or worse are writing raw sql into your programs, then I strongly encourage you to give SA a shot in your next project. I plan to do a series of blog posts which highlight how SA can be leveraged as part of architecture which is both powerful and easy to maintain.