【问题标题】:Flask SQLAlchemy WAS not migrating updates to db table model (suggested fix)Flask SQLAlchemy WAS 未将更新迁移到数据库表模型(建议修复)
【发布时间】:2020-09-02 01:35:35
【问题描述】:

我在使用 Flask-SQLALchemy 库将更新迁移到我的表模型时遇到了困难

对我的表架构进行更改后,我运行了 migrate 命令并且响应显示没有更改...

$ python manage.py db 迁移

> INFO  [alembic.runtime.migration] Context impl PostgresqlImpl. 

> INFO  [alembic.runtime.migration] Will assume transactional DDL. 

> INFO  [alembic.env] No changes in schema detected.

models.py 文件

from sqlalchemy.dialects.postgresql import JSON
from app import db


class Result(db.Model):
    __tablename__ = 'results'

    id = db.Column(db.Integer, primary_key=True)
    url = db.Column(db.String())
    datetime = db.Column(db.String())
    results = db.Column(JSON)
    errors = db.Column(JSON)


    def __init__(self, url, datetime, results, errors=None):
        self.url = url
        self.datetime = datetime
        self.results = results
        self.errors = errors


    def __repr__(self):
        return f"<id {self.id}>"

manage.py 文件

import os
import json

from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand

from app import app, db


app.config.from_object(os.environ['APP_SETTINGS'])

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

manager.add_command('db', MigrateCommand)


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

【问题讨论】:

    标签: python flask sqlalchemy model migrate


    【解决方案1】:

    @dina-taklit 的这个答案对我有用...answer to "Flask-Migrate No Changes Detected to Schema on first migration"

    1.(来自 Python REPL)导入迁移模型:

    from flask_migrate import Migrate
    

    2.导入app、db对象:

    from app import app, db
    

    3.启动迁移类:

    migrate = Migrate(app, db)
    

    4.运行以下命令:

    db.create_all()
    

    5.(来自 psql CLI)删除数据库

    DROP DATABASE db_name;
    

    6.再创建一次 =>

    CREATE DATABSE db_name OWNER owner_name;
    

    7.(来自 Ubuntu CLI)将环境变量指向您的烧瓶应用程序文件 =>

    export FLASK_APP=name_app.py
    

    8.运行迁移

    python manage.py db migrate
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      • 2020-07-21
      • 2019-08-10
      • 1970-01-01
      • 2013-10-13
      相关资源
      最近更新 更多