作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我正在使用 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 升级
但是,在尝试 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 升级到最新版本。
最佳答案
以防万一以后有人遇到这个问题--
我没有 migrations 文件夹,因为我已经从另一个项目传输了 DB 文件。所以,当您没有正常运行的迁移文件夹时,就会发生这种情况!
关于python - Flask-Migrate "No section: ' alembic '"on "db migrate”命令,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30056879/
我是一名优秀的程序员,十分优秀!