我正在尝试为SQLalchemy实施Flask管理器。
当我运行python run.py db migrate
然后python run.py db upgrade
时,我的表格没有受到影响。我刚从models.py文件中删除了一个应从表中删除的字段。
以下是我的文件:
root / run.py:
#!flask/bin/python
import sys
sys.path.append("/app")
from app import app
app.run(debug = True)
root / app / __ init __ .py:
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.script import Manager
from flask.ext.migrate import Migrate, MigrateCommand
from app.database import db, db_session, init_db
app = Flask(__name__)
app.config.from_object('settings')
init_db()
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
from catalog.views import catalog
app.register_blueprint(catalog)
root / app / database.py:
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('mysql://root:root@127.0.0.1/mydb', convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
db = declarative_base()
db.query = db_session.query_property()
def init_db():
import app.models
db.metadata.create_all(bind=engine)
我认为我对manager.run()
做错了,但我并不清楚如何运行它。
编辑:
在iurisilvio的建议之后,我终于做了更简单的数据库设置:
root / app / __ init __ .py:
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
from flask.ext.script import Manager
from flask.ext.migrate import Migrate
app = Flask(__name__)
app.config.from_object('settings')
app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://root:root@127.0.0.1/mydb'
db = SQLAlchemy()
db.app = app
db.init_app(app)
migrate = Migrate(app, db)
manager = Manager(app)
from catalogue.views import catalogue
app.register_blueprint(catalogue)
root / run.py:
#!flask/bin/python
import sys
sys.path.append("/app")
from app import app, manager
from flask.ext.migrate import MigrateCommand
manager.add_command('db', MigrateCommand)
app.debug = True
manager.run()
现在它工作得很好!
答案 0 :(得分:1)
Flask-Migrate适用于Flask-SQLAlchemy会话。您正在使用原始sqlalchemy创建会话。
我不知道是否有可能。您必须更改database.py才能使用Flask-SQLAlchemy。