MYSQLAlchemy / Flask无法识别迁移

时间:2019-02-05 10:08:40

标签: python flask flask-sqlalchemy

我是Flask / MYSQLAlchemy / Postgres的新手,并且仍在尝试了解所有内容。

我有一个应用程序:

from flask import Flask
from flask_cors import CORS
import sentry_sdk
from sentry_sdk.integrations.flask import FlaskIntegration


def create_app(*args, app_name="SMARRTBROKER_API"):
    # add sentry integration
    sentry_sdk.init(
        dsn="https://cd31f9dbcd56420fa0ca08422a441747@sentry.io/1369455",
        integrations=[FlaskIntegration()]
    )
    app = Flask(app_name)
    CORS(app, resources=r'/api/*')
    # CORS(app, resources={r"/api/*": {"origins": "*"}}, supports_credentials=True)
    app.config.from_object('api.config.DevelopmentConfig')

    from api.api import api
    app.register_blueprint(api, url_prefix='/api')
    # app.config['SQLALCHEMY_ECHO'] = True

    from api.models import db
    db.init_app(app)

    return app

还有一个manage.py文件:

from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand

from api.application import create_app
from api.models import db, Lead, User, Setting, Category, Keyword, keyword_identifier, Product, Plan
from filter_class import Filter
from filter_keyword import FilterKeyword
from sqlalchemy.dialects.postgresql import array
from sqlalchemy import or_, not_
from sqlalchemy import any_
from datetime import datetime, timedelta
from sqlalchemy.orm.attributes import flag_modified
from classification import Classification

app = create_app()

migrate = Migrate(app, db)
manager = Manager(app)

# provide a migration utility command
manager.add_command('db', MigrateCommand)


if __name__ == '__main__':
    manager.run()

还有一个model.py文件:

"""
models.py
Data classes for the api application
"""

from datetime import datetime
from flask_sqlalchemy import SQLAlchemy
from werkzeug.security import generate_password_hash, check_password_hash
from sqlalchemy.ext.mutable import Mutable
from sqlalchemy.dialects.postgresql import ARRAY
import enum
import locale

db = SQLAlchemy()

...


class Setting(db.Model):
    __tablename__ = 'settings'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Text, nullable=False)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow)
    import_settings = db.Column(db.JSON, nullable=True)

    def to_dict(self):
        return dict(
            id=self.id,
            name=self.name,
            created_at=self.created_at,
            label=self.label
        )


class Keyword(db.Model):
    __tablename__ = 'keywords'

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.Text, nullable=False)
    label = db.Column(db.Text, nullable=False, unique=True)
    created_at = db.Column(db.DateTime, default=datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.utcnow)
    leads = db.relationship('Lead', secondary=keyword_identifier)

    def to_dict(self):
        return dict(
            id=self.id,
            label=self.label,
            name=self.name,
            created_at=self.created_at
        )

...

直到几个星期前,如果我对模型文件进行了任何更改,我都可以运行python manage.py db migratepython manage.py db upgrade,并且对模型进行了修改,并且迁移可以顺利进行。现在,无论出于什么原因,当我运行migration / upgrade命令时,什么都没有发生,而且看来根本看不到我的更改。有什么想法吗?

0 个答案:

没有答案