简体   繁体   English

flask-sqlalchemy对特定表使用drop_all和create_all

[英]flask-sqlalchemy use of drop_all and create_all for specific tables

In sqlalchemy (0.8.2), drop_all() and create_all() both have a tables parameter, which can be a list of Table objects to drop or add. drop_all() (0.8.2)中, drop_all()create_all()都有一个tables参数,该参数可以是要删除或添加的Table对象的列表。

In flask-sqlalchemy (1.0) these methods do not have this parameter. 在flask-sqlalchemy(1.0)中,这些方法没有此参数。

What is the appropriate way to drop / create a subset of database tables, using flask-alchemy? 使用flask-alchemy删除/创建数据库表子集的适当方法是什么?

Flask-SQLAlchemy's create_all() method will use the Base's metadata to create the table, by calling SQLAlchemy's MetaData.create_all() method. Flask-SQLAlchemy的create_all()方法将使用Base的元数据来创建表,方法是调用SQLAlchemy的MetaData.create_all()方法。 This method allows for a list of table objects to specify. 此方法允许指定表对象的列表。 You will also need to provide it a "bind", which is basically the engine. 你还需要提供一个“绑定”,它基本上就是引擎。 You can retrieve that using the engine property of Flask-SQLAlchemy. 您可以使用Flask-SQLAlchemy的引擎属性检索它。

So, you should be able to do... 那么,你应该能做到......

db = SQLAlchemy(app)

class MyTable(db.Model):
    ...

class MyOtherTable(db.Model):
    ...

db.metadata.create_all(db.engine, tables=[
    MyTable.__table__,
    ...
])

This assumes that you are using a single database. 这假设您使用的是单个数据库。 Otherwise you would need to do a create_all() call for each database using get_engine() . 否则,您需要使用get_engine()为每个数据库执行create_all()调用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM