James King James King - 3 months ago 43
MySQL Question

Questions about using Flask+MySQL+Flask-SQLAlchemy

I am going to build a site using Flask+MySQL+Flask-SQLAlchemy, however, after reading some tutorials, I have some questions:

  • Flask-SQLAlchemy can be imported in at least two ways:


from flask.ext.sqlalchemy import SQLAlchemy

OR http://flask.pocoo.org/docs/patterns/sqlalchemy/

from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base

The first way seems much more convenient. Why Pocoo team choose to use the second way?

I need some good examples on MySQL+Flask-SQLAlchemy, while most example are SQLite+MySQL+Flask-SQLAlchemy.


Flask-SQLAlchemy was written by Armin (the creator of Flask) to make it simple to work with SQLAlchemy. The pattern described in Flask's documentation is what you would use if you did not choose to use the Flask-SQLAlchemy extension.

As for MySQL vs. SQLite, the whole point of the SQLAlchemy ORM is to make it possible (for the most part) to ignore what database you are running against. SomeModel.filter(SomeModel.column == 'value') will work the same, regardless of what database you are connecting to.