Recently i started to use flask and I liked pretty much. In the past I had a system in PHP with a lot of databases like marketing, HR, finance and so on. Each of this databases had their own tables like HR used to have employers, companies and so on.
Each of this tables was a class in PHP, we used this system to facilitate save/delete since they were used all over the system all we had to do was instantiate a new object from one of the table/class where which column was a object property and then call $obj->Save() to insert a new row in the table.
Programming has evolved so much since then so my doubt is if there’s a more efficient way to do that in python/flask, instead of creating a class for each of the tables from the databases like I used to do in PHP, I know this is a large question so I would appreciate recommendations of books, wikis and so on about this topic.
Advertisement
Answer
A fairly modern approach to interface with a database in high-level programming languages is to use an ORM, or Object Relational Mapper. See this Stack Overflow thread for a good explanation.
If you are using Flask, SQLAlchemy is the most popular choice, so much so that Flask actually has an extension called Flask-SQLAlchemy. Keep in mind, that you will still be mapping classes to database entities. However, the power of SQLAlchemy is that it provides a higher level of abstraction on top of the database, which can go beyond simply mapping a class to a table row. According to the documentation:
SQLAlchemy considers the database to be a relational algebra engine, not just a collection of tables. Rows can be selected from not only tables but also joins and other select statements; any of these units can be composed into a larger structure. SQLAlchemy’s expression language builds on this concept from its core.
This Stack Overflow thread provides more Python ORM suggestions.