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

…衆ロ難τιáo~ 提交于 2019-12-04 00:11:38

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!