Python 无法使用 Flask-Migrate (Alembic) 迁移或升级数据库
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32798937/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't migrate or upgrade database with Flask-Migrate (Alembic)
提问by Pav Sidhu
I've been using Flask-Migrate (Alembic) for updating my database. I updated my models.py
file however I made an error. I ran a migration and went to upgrade the database, however I got this error:
我一直在使用 Flask-Migrate (Alembic) 来更新我的数据库。我更新了我的models.py
文件,但是我犯了一个错误。我运行了一个迁移并去升级数据库,但是我收到了这个错误:
sqlalchemy.exc.IntegrityError: (_mysql_exceptions.IntegrityError) (1215, 'Cannot add foreign key constraint') [SQL: u'\nCREATE TABLE topics (\n\tid INTEGER NOT NULL AUTO_INCREMENT, \n\t`subjectID` INTEGER, \n\ttopic VARCHAR(150) NOT NULL, \n\tPRIMARY KEY (id), \n\tFOREIGN KEY(`subjectID`) REFERENCES subjects (id)\n)\n\n']
What I had done was have db.Text
instead of db.Integer
for a foreign key.
我所做的是拥有db.Text
而不是db.Integer
外键。
When I try run a new migration I get this:
当我尝试运行一个新的迁移时,我得到了这个:
alembic.util.CommandError: Target database is not up to date.
So now I'm stuck. I cannot upgrade the database nor run a migration. I tried to downgrade to an older database version by using something like this:
所以现在我被困住了。我无法升级数据库或运行迁移。我尝试使用以下方法降级到较旧的数据库版本:
python manage.py db downgrade --sql b877018671c:36949b1cca31
But when I run python manage.py db current
I get the newest database version which I am stuck in.
但是当我运行时,python manage.py db current
我得到了我卡在其中的最新数据库版本。
Is there a fix for this? Thanks.
有没有办法解决这个问题?谢谢。
采纳答案by Kelly Keller-Heikkila
Alembic stores the db version in a table it creates called alembic_version
. This table contains a single field and row alembic_version.version_num
. Make sure the value for this matches the filename of the most recent file in migrations/version
. This version number is also contained inside the revision file in the revision
variable that generally shows up on line 26 of the file. Make sure it matches the db version.
Alembic 将 db 版本存储在它创建的名为alembic_version
. 该表包含单个字段和行alembic_version.version_num
。确保此值与migrations/version
. 此版本号也包含在修订文件中的revision
变量中,该变量通常显示在文件的第 26 行。确保它与数据库版本匹配。
Another option is to simply drop the db and recreate it using alembic. If this is a development environment, where the data is not important, that would be my recommendation.
另一种选择是简单地删除数据库并使用 alembic 重新创建它。如果这是一个数据不重要的开发环境,那将是我的建议。
回答by Taehan Stott
I feel like the accepted answer is a little over-complicated. I had this same issue and the way that I solved it was to simply delete the migration that contained the coding errors. You don't need it anyways since, again, it was coded incorrectly. Find the latest migration in the migrations/versions
folder, delete it, then run your migration again and upgrade. You don't need to delete the data in your database just to migrate it.
我觉得接受的答案有点过于复杂。我遇到了同样的问题,我解决它的方法是简单地删除包含编码错误的迁移。无论如何您都不需要它,因为它再次被错误地编码。在migrations/versions
文件夹中找到最新的迁移,将其删除,然后再次运行迁移并升级。您不需要为了迁移数据库中的数据而删除它。
回答by Jun
alembic.util.CommandError: Target database is not up to date.
alembic.util.CommandError:目标数据库不是最新的。
Could you try following steps?
您可以尝试以下步骤吗?
python manage.py db stamp head
python manage.py db migrate
python manage.py db upgrade
'stamp' the revision table with the given revision; don't run any migrations
用给定的修订版“标记”修订表;不要运行任何迁移