flask-sqlalchemy对特定表使用drop_all和create_all [英] flask-sqlalchemy use of drop_all and create_all for specific tables

查看:395
本文介绍了flask-sqlalchemy对特定表使用drop_all和create_all的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在sqlalchemy(0.8.2)中,drop_all()create_all()都有一个tables参数,该参数可以是要删除或添加的Table对象的列表.

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.

在flask-sqlalchemy(1.0)中,这些方法没有此参数.

In flask-sqlalchemy (1.0) these methods do not have this parameter.

使用flask-alchemy删除/创建数据库表子集的合适方法是什么?

What is the appropriate way to drop / create a subset of database tables, using flask-alchemy?

推荐答案

Flask-SQLAlchemy的 MetaData.create_all() 方法.此方法允许指定表对象的列表.您还需要为其提供绑定",它基本上是引擎.您可以使用 engine 进行检索Flask-SQLAlchemy的属性.

Flask-SQLAlchemy's create_all() method will use the Base's metadata to create the table, by calling SQLAlchemy's MetaData.create_all() method. 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.

所以,您应该可以...

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().

这篇关于flask-sqlalchemy对特定表使用drop_all和create_all的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆