问题标签 [flask-migrate]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
1 回答
1484 浏览

python - Flask-Migrate,如果存在则不创建表

我有一个带有MySQL数据库的 Flask 项目,并使用SQLAlchemy作为 ORM 和Flask-Migrate * 进行迁移。

我编写了我的模型,当我运行迁移时,迁移文件是空的,因为现有表超出了Flask-Migrate 的控制,所以我实际上必须删除这些表以让迁移工具检测并再次创建它们。但问题是我不想删除和创建我的表。

那么有没有一种方法可以让我的模型与我现有的表同步?

编辑: 我刚刚发现在 env.py 文件中,可以指定存在的表并且它不会创建这些表:

感谢你的回答。

0 投票
1 回答
1793 浏览

python - Flask db migrate 不会影响我的数据库表

我正在尝试为 SQLalchemy 实现 Flask 管理器。当我运行python run.py db migratethenpython run.py db upgrade时,我的表不受影响。我刚刚从我的 models.py 文件中删除了一个应该从表中删除的字段。

这是我的文件:

根/run.py:

根/应用程序/ __初始化__ .py :

根/app/database.py:

我认为我做错了,manager.run()但我不清楚如何运行它。

编辑

在 iurisilvio 的建议下,我终于做了更简单的数据库设置:

根/应用程序/ __初始化__ .py :

根/run.py:

现在它工作得很好!

0 投票
1 回答
1949 浏览

flask - 烧瓶迁移多个模型.py

我有一个与 Flask-migrate 相关的问题。我正在使用 Flask 创建一组 Web 服务。我已经在 python 应用程序中将每个 Web 服务拆分到他自己的包中。

应用程序结构如下所示:

MyApp WS1 models.py WS2 models.py CommonPackage models.py

如何导入所有模块并初始化数据库?我试图手动导入它们,但不工作。我知道如果我分别从 WS1 或 Ws2 导入“应用程序”,它会起作用,但我想在一次操作中做到这一点,这可能吗?

在这里您可以找到 Flask-migrate 的代码:

这是我初始化 SQLAlchemy 会话的模块:

所有模型都导入此模块并从 Base 继承。

恩里科

0 投票
1 回答
466 浏览

python - flask-migrate wants to drop my indeces

I've got a Flask app with the following models:

I've run a migration to specify my indices and foreign key constraints:

However, any time I create another new migration Alembic seems to want to drop my indexes.

Is there a way to freeze Alembic's autogeneration at the current DB/Model schema?

0 投票
1 回答
790 浏览

python - 将 Flask-Migrate 与 Flask-Security 一起使用

我正在尝试让一个基本的 Flask-Security 应用程序与 Flask-Migrate 一起使用。我有两个主要的 py 文件:app.py 和 db_migrate.py

应用程序.py:

db_migrate.py:

我已经运行了迁移工具来初始化和迁移数据库一次,以创建一个新的数据库,它起作用了:

我试图运行 app.py。它在 localhost 上正常运行,但是当我尝试登录用户时,我收到以下 OperationalError:

OperationalError: (OperationalError) no such table: user u'SELECT user.id AS user_id, user.email AS user_email, user.password AS user_password, user.active AS user_active, user.confirmed_at AS user_confirmed_at, user.favcolor AS user_favcolor \nFROM用户\nWHERE lower(user.email) LIKE lower(?)\n LIMIT ? 抵消 ?' (u'xxx@xxx.com', 1, 0)

基本上,我怀疑我正在创建user_datastore并且security正确,因为我可能不应该以这种方式导入User-Role但我不确定如何正确访问它们。

编辑:

由于建议,我添加了这个最终命令:

但是,现在当我尝试通过电子邮件确认用户注册时出现此错误:

(InvalidRequestError: Object '' is already attached to session '1' (this is '3')

0 投票
5 回答
66243 浏览

python - 无法为映射表组装任何主键列

当我尝试创建数据库架构迁移时,我遇到了这个奇怪的错误。你能帮我找出问题所在吗?

我的模型:

0 投票
2 回答
873 浏览

python - 当我使用 ForeignKey 添加模型时,flask-migrate 不起作用

我有一个数据库仍然存在。然后我将模型添加到models.py:

然后我运行迁移脚本,它调用错误:

NoReferencedTableError: Foreign key associated with column 'pub_square.author_id' could not find table 'user' with which to generate a foreign key to target column 'id'

在这之前,我可以成功运行几次迁移脚本。但是这一次,当它引用外键关系时,它就不起作用了。

为了证明我的模型代码是正确的,我重新创建了数据库,它可以工作。所以,这是对该错误的烧瓶迁移调用。

0 投票
1 回答
2134 浏览

flask - 如何在 pythonanywhere 上使用 flask-migrate?

有人有在 pythonanywhere 上成功使用 flask-migrate 的例子吗?有没有人有一个简单的例子来说明使用迁移的 app.py 应该是什么样子?类似于以下内容:

我发现当我尝试跑步时

它无法生成迁移存储库。这可能是 pythonanywhere 上的文件权限问题吗?

0 投票
3 回答
8872 浏览

python - flask-migrate 不检测模型

我在这里阅读(和观看)有关 Flask-Migrate 的信息: https ://realpython.com/blog/python/flask-by-example-part-2-postgres-sqlalchemy-and-alembic/ 和这里https://www .youtube.com/watch?v=YJibNSI-iaE#t=21

并按照本教程执行所有操作:

  • 我启动了一个本地 postgres 服务器(使用 Postgres.App,它在 postgresql://localhost:5432 启动服务器)
  • 根据上述教程更新配置
  • 更新了 app.py,创建了 models.py 等。

安装 Flask-Migrate 并运行后

它应该检测在models.py 中声明的所有表。

就我而言,它什么也没检测到。而且,根据对教程的评论,这不仅仅是我的情况。那么,我该如何进行这项工作呢?

0 投票
1 回答
2241 浏览

flask - Why is Flask-Migrate making me do a 2-steps migration?

I'm working on a project with Flask, SQLAlchemy, Alembic and their wrappers for Flask (Flask-SQLAlchemy and Flask-Migrate). I have four migrations:

When I start a new and clean database and try to run the migrations I get an error:

If I ask Flask-Migrate to run all migrations but the last, it works. If after that I run the upgrade command again, it works – that is, it fully upgrades my database without a single change in code:

TL;DR

The last migration (Feed: Countries) run queries on the table fed by the previous one (Feed: Continents). If I have the continents table create and fed, the scripts should work. But it doesn't. Why do I have to stop the migration process between then to re-start it in another command? I really don't get this. Is it some command Alembic executes after a serie of migrations? Any ideas?

Just in case

My models are defined as follows:

Many thanks,

UPDATE 1: The upgrade method of the last two migrations

As @Miguel asked in a comment, here there are the upgrade methods of the last two migrations:

Feed: Continents

Feed: Countries (which depends on the table fed on the last migration)

The CSV I'm using are basically following this patterns:

continents.csv

iso3166.csv

_country_continent.csv_

So Feed: Continents feeds the continent table, and Feed: Countries feeds the country table. But it has to query the continents table in order to make the proper link between the country and the continent.

UPDATE 2: Some one from Reddit already offered an explanation and a workaround

I asked the same question on Reddit, and themathemagician said:

I've run into this before, and the issue is that the migrations don't execute individually, but instead alembic batches all of them (or all of them that need to be run) and then executes the SQL. This means that by the time the last migration is trying to run, the tables don't actually exist yet so you can't actually make queries. Doing

This isn't the most elegant solution (and that was for Postgres, the command may be different for other dbs), but it worked for me. Also, this isn't actually an issue with Flask-Migrate as much as an issue with alembic, so if you want to Google for more info, search for alembic. Flask-Migrate is just a wrapper around alembic that works with Flask-Script easily.