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

后端 未结 1 1430
花落未央
花落未央 2021-01-04 02:55

I am a little confused with the topic alluded to in the title.

So, when a Flask app is started, does the SQLAlchemy search theSQLALCHEMY_DATABASE_URI f

相关标签:
1条回答
  • 2021-01-04 03:07

    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.

    0 讨论(0)
提交回复
热议问题