Flask-sqlalchemy`create_all()`,指定模型

时间:2012-11-18 12:24:59

标签: python flask flask-sqlalchemy

我有带命令的文件,与模型分开。我将所有模型从models导入到starter文件后才执行模型。

db = SQLAlchemy(app)                      

from models import *                     

try:                                      
    argv = sys.argv[1]                    
    argv == '--run' and app.run()         
    argv == '--create' and db.create_all()

但对我来说这很模糊。我走遍了Flask-Sqlalchemy源代码。我看到了:

def get_tables_for_bind(self, bind=None):
    """Returns a list of all tables relevant for a bind."""
    result = []
    for table in self.Model.metadata.tables.itervalues():
        if table.info.get('bind_key') == bind:
            result.append(table)
    return result

我理解self.Model是:

def make_declarative_base(self):
    """Creates the declarative base."""
    base = declarative_base(cls=Model, name='Model',
                            mapper=signalling_mapper,
                            metaclass=_BoundDeclarativeMeta)
    base.query = _QueryProperty(self)
    return base

我是否有更明确的方法来指定执行模型?以及self.Model如何知道必须执行哪些表(导入后)?

0 个答案:

没有答案