Flask-SQLAlchemy - When are the tables/databases created and destroyed? Flask-SQLAlchemy - When are the tables/databases created and destroyed? flask flask

Flask-SQLAlchemy - When are the tables/databases created and destroyed?


Tables are not created automatically; you need to call the SQLAlchemy.create_all() method to explicitly to have it create tables for you:

db = SQLAlchemy(app)db.create_all()

You can do this with command-line utility, for example. Or, if you deploy to a PaaS such as Google App Engine, a dedicated admin-only view.

The same applies for database table destruction; use the SQLAlchemy.drop_all() method.

See the Creating and Dropping tables chapter of the documentation, or take a look at the database chapter of the Mega Flask Tutorial.

You can also delegate this task to Flask-Migrate or similar schema versioning tools. These help you record and edit schema creation and migration steps; the database schema of real-life projects is never static and you would want to be able to move existing data between versions or the schema. Creating the initial schema is then just the first step.