我正在尝试为应用设置Flask-migrate,但第一个迁移文件始终包含SQL以删除所有表而不是构建它们。
这是我的应用结构:
# app/__init__.py
from flask import Flask
from flask.ext.sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)
# app/models.py
from . import db
class Location(db.Model):
__tablename__ = 'locations'
id = db.Column(db.Integer, primary_key=True)
city = db.Column(db.String(80), unique=True, nullable=False)
city_url_slug = db.Column(db.String(80), unique=True, nullable=False)
hotels = db.relationship('Hotel', backref='location', lazy='dynamic')
def __repr__(self):
return self.city
class Hotel(db.Model):
__tablename__ = 'hotels'
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(200), nullable=False)
url = db.Column(db.String(80))
free_parking = db.Column(db.Boolean())
self_parking = db.Column(db.Boolean())
# manage.py
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand
from app import app, db
# db migrate
migrate = Migrate(app, db)
manager = Manager(app)
manager.add_command('db', MigrateCommand)
if __name__ == '__main__':
manager.run()
我通过进入shell并运行db.create_all()来创建我的数据库。然后我运行python manage.py db init,然后运行python manage.py db migrate。但结果是一个包含以下内容的迁移文件:
def upgrade():
op.drop_table('locations')
op.drop_table('hotels')
如何修复我的代码,以便检测到不需要进行任何更改,还是需要构建所有表和字段?