Flask-Migrate“db migrate”命令中的“No section:'alembic'”

时间:2015-05-05 15:13:40

标签: python flask flask-sqlalchemy alembic flask-migrate

我正在使用Flask-Migrate,我正在尝试使用db migrate命令向我的数据库添加一列。这是设置:

from flask import Flask, render_template, request, session, flash, redirect, url_for
from flask.ext.babel import Babel
from flask.ext.mail import Mail
from flask.ext.script import Manager
from flask.ext.migrate import Migrate, MigrateCommand
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.security import Security, SQLAlchemyUserDatastore, \
     UserMixin, RoleMixin, login_required, roles_required
from flask.ext.login import current_user
from forms import UserEditForm
import os, sys, datetime, random, math
basedir = os.path.abspath(os.path.dirname(__file__))

# Create app
application = Flask(__name__)
application.config['DEBUG'] = True
application.config['SECRET_KEY'] = 'super-secret'
application.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.join(basedir, 'app.db')

application.config['DEFAULT_MAIL_SENDER'] = 'info@site.com'
application.config['SECURITY_REGISTERABLE'] = True
application.config['SECURITY_CONFIRMABLE'] = True
application.config['SECURITY_RECOVERABLE'] = True

application.config.from_object('config.email')

application.config['DBPATH'] = os.path.join(basedir, 'app.db')

# Setup mail extension
mail = Mail(application)

# Setup babel
babel = Babel(application)

@babel.localeselector
def get_locale():
    override = request.args.get('lang')

    if override:
        session['lang'] = override

    rv = session.get('lang', 'en')
    return rv

# Create database connection object

db = SQLAlchemy(application)
migrate = Migrate(application, db)

manager = Manager(application)
manager.add_command('db', MigrateCommand)

# Define models
roles_users = db.Table('roles_users',
        db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
        db.Column('role_id', db.Integer(), db.ForeignKey('role.id')))

class Role(db.Model, RoleMixin):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)
    description = db.Column(db.String(255))

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(255), unique=True)
    password = db.Column(db.String(255))
    active = db.Column(db.Boolean())
    confirmed_at = db.Column(db.DateTime())
    nickname = db.Column(db.String(255))
    favcolor = db.Column(db.String(255))
    favshape = db.Column(db.String(255))
    favflower = db.Column(db.String(255))
    favband = db.Column(db.String(255)) # <<<--- NEW COLUMN

    roles = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))

    def __str__(self):
        return '<User id=%s email=%s>' % (self.id, self.email)

user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(application, user_datastore)

# Views
@app.route('/')
def home():
    return render_template('index.html')


if __name__ == '__main__':
    if str(sys.argv[1]) == 'run': 
        application.run(debug=True) #(host='0.0.0.0', debug=True)
    else:
        manager.run()

我正在尝试使用以下命令添加新列favband

flask\Scripts\python.exe application.py db migrate flask\Scripts\python.exe application.py db upgrade

但是,在尝试db migrate时,我收到以下错误:

Traceback (most recent call last):
  File "application.py", line 398, in <module>
    manager.run() #1) db migrate, 2) db upgrade
  File "C:\website\flask\lib\site-packages\flask_script\__init__.py", line 412, in run
    result = self.handle(sys.argv[0], sys.argv[1:])
  File "C:\website\flask\lib\site-packages\flask_script\__init__.py", line 383, in handle
    res = handle(*args, **config)
  File "C:\website\flask\lib\site-packages\flask_script\commands.py", line 216, in __call__
    return self.run(*args, **kwargs)
  File "C:\website\flask\lib\site-packages\flask_migrate\__init__.py", line 132, in migrate
    config = _get_config(directory)
  File "C:\website\flask\lib\site-packages\flask_migrate\__init__.py", line 46, in _get_config
    config.set_main_option('script_location', directory)
  File "C:\website\flask\lib\site-packages\alembic\config.py", line 198, in set_main_option
    self.file_config.set(self.config_ini_section, name, value)
  File "C:\Python27\Lib\ConfigParser.py", line 701, in set
    ConfigParser.set(self, section, option, value)
  File "C:\Python27\Lib\ConfigParser.py", line 388, in set
    raise NoSectionError(section)
ConfigParser.NoSectionError: No section: 'alembic'

我也在flask\Scripts内尝试过此操作,但得到同样的错误:

python.exe ../../application.py db migrate

我也尝试将flask-migrate升级到最新版本。

2 个答案:

答案 0 :(得分:4)

以防万一将来有这个问题 -

我没有迁移文件夹,因为我从其他项目传输了DB文件。因此,当您没有正常运行的迁移文件夹时会发生这种情况!

答案 1 :(得分:2)

我也遇到了这个问题。我有一个与预期文件夹不同的迁移文件夹。通常,您可以执行以下操作,它将适当地找到目录:

migrate = Migrate(app, db, directory="/path/to/migrations_folder")

但是,如果您实例化Migrate对象,然后稍后使用db和application初始化它将无法工作:

# This won't work
migrate = Migrate(directory='/path/to/migrations_folder')
migrate.init_app(app, db)

init_app会将目录覆盖回默认值。因此,您需要始终在init_app调用中传递目录。

# This will
migrate = Migrate()
migrate.init_app(app, db, directory='/path/to/migrations_folder')

编辑我提交了fix