我是参照flask web开发这本书,数据库为mysql,说到flask-migrate,我在用命令 python myblog.py db migrate -m “initial migration”创建数据库迁移脚本时报以下错误
(venv) zhaoyb@zhaoyb-pc:~/PycharmProjects/myblog$ python myblog.py db migrate -m "initial migration"
/home/zhaoyb/PycharmProjects/myblog/venv/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py:800: UserWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True to suppress this warning.
warnings.warn('SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True to suppress this warning.')
Traceback (most recent call last):
File "myblog.py", line 77, in <module>
manager.run()
File "/home/zhaoyb/PycharmProjects/myblog/venv/lib/python3.5/site-packages/flask_script/__init__.py", line 412, in run
result = self.handle(sys.argv[0], sys.argv[1:])
File "/home/zhaoyb/PycharmProjects/myblog/venv/lib/python3.5/site-packages/flask_script/__init__.py", line 383, in handle
res = handle(*args, **config)
File "/home/zhaoyb/PycharmProjects/myblog/venv/lib/python3.5/site-packages/flask_script/commands.py", line 216, in __call__
return self.run(*args, **kwargs)
File "/home/zhaoyb/PycharmProjects/myblog/venv/lib/python3.5/site-packages/flask_migrate/__init__.py", line 173, in migrate
version_path=version_path, rev_id=rev_id)
File "/home/zhaoyb/PycharmProjects/myblog/venv/lib/python3.5/site-packages/alembic/command.py", line 117, in revision
script_directory.run_env()
File "/home/zhaoyb/PycharmProjects/myblog/venv/lib/python3.5/site-packages/alembic/script/base.py", line 407, in run_env
util.load_python_file(self.dir, 'env.py')
File "/home/zhaoyb/PycharmProjects/myblog/venv/lib/python3.5/site-packages/alembic/util/pyfiles.py", line 93, in load_python_file
module = load_module_py(module_id, path)
File "/home/zhaoyb/PycharmProjects/myblog/venv/lib/python3.5/site-packages/alembic/util/compat.py", line 68, in load_module_py
module_id, path).load_module(module_id)
File "<frozen importlib._bootstrap_external>", line 388, in _check_name_wrapper
File "<frozen importlib._bootstrap_external>", line 809, in load_module
File "<frozen importlib._bootstrap_external>", line 668, in load_module
File "<frozen importlib._bootstrap>", line 268, in _load_module_shim
File "<frozen importlib._bootstrap>", line 693, in _load
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "migrations/env.py", line 22, in <module>
current_app.config.get('SQLALCHEMY_DATABASE_URI'))
File "/home/zhaoyb/PycharmProjects/myblog/venv/lib/python3.5/site-packages/alembic/config.py", line 218, in set_main_option
self.set_section_option(self.config_ini_section, name, value)
File "/home/zhaoyb/PycharmProjects/myblog/venv/lib/python3.5/site-packages/alembic/config.py", line 245, in set_section_option
self.file_config.set(section, name, value)
File "/usr/lib/python3.5/configparser.py", line 1190, in set
super().set(section, option, value)
File "/usr/lib/python3.5/configparser.py", line 891, in set
value)
File "/usr/lib/python3.5/configparser.py", line 401, in before_set
"position %d" % (value, tmp_value.find('%')))
ValueError: invalid interpolation syntax in 'mysql+pymysql://××××××:×××××××××××@localhost/myblog' at position 27
这是我的程序代码
from flask import Flask, render_template, flash
from flask_wtf import Form
from wtforms import StringField, SubmitField, PasswordField
from wtforms.validators import Required
from flask_script import Manager
from flask_bootstrap import Bootstrap
from flask_sqlalchemy import SQLAlchemy
from flask_script import Shell
from flask_migrate import Migrate, MigrateCommand
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hard to guess string'
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql+pymysql://××××××:××××××××××@localhost/myblog'
app.config['SQLALCHEMY_COMMIT_ON_TEARDOWN'] = True
app.config['DEBUG'] = True
bootstrap = Bootstrap(app)
db = SQLAlchemy(app)
manager = Manager(app)
migrate = Migrate(app, db)
manager.add_command('db', MigrateCommand)
def make_shell_context():
return dict(app=app, db=db, User=User, Role=Role)
class User(db.Model):
__tablename__ = 'users'
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(64), unique=True, index=True)
role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
def __repr__(self):
return '<User %r>' % self.username
class Role(db.Model):
__tablename__ = 'roles'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), unique=True)
users = db.relationship('User', backref='role')
def __repr__(self):
return '<Role %r>' % self.name
class LoginForm(Form):
username = StringField('用户名', validators=[Required()])
password = PasswordField('密码', validators=[Required()])
login = SubmitField('登录')
@app.route('/', methods=['GET', 'POST'])
def index():
form = LoginForm()
return render_template('index.html', form=form, is_active=True)
@app.route('/tweet')
def tweet():
form = LoginForm()
return render_template('tweet.html', is_actived=True, form=form)
@app.errorhandler(404)
def page_not_found(e):
return render_template('404.html'), 404
@app.errorhandler(404)
def page_not_found(e):
return render_template('500.html'), 500
manager.add_command('shell', Shell(make_context=make_shell_context))
if __name__ == '__main__':
manager.run()
请大家帮我看看
………………………………………………………………………………………………………………
问题原因已找到,书的作者给了回答
竟然是mysql密码里面包含%
(话说这个问题真的很难想到,希望大家引以为戒)
说的很清楚:
你的URI格式有问题, 建议你用标准的格式: http://flask-sqlalchemy.pocoo...
标准格式的话你需要安装mysqldb, 有点小坑, 安装可以参考下面2个链接
http://stackoverflow.com/ques...
http://stackoverflow.com/ques...