17

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:

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.

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.

Is there a fix for this? Thanks.

Pav Sidhu
  • 6,724
  • 18
  • 55
  • 110
  • 3
    It sounds like you have a migration (the bad one) that you haven't run yet. Either edit it to be correct or delete it and generate the migration again. – dirn Sep 26 '15 at 18:48
  • How do I delete a migration? – Pav Sidhu Sep 26 '15 at 19:18
  • 2
    Migrations are just files. You just have to delete the file. Flask-Migrate typically stores them inside `migrations/version`. – dirn Sep 26 '15 at 19:22
  • I found the latest migration file and deleted it, however when I try and run a migration, I still manage to get `Target database is not up to date.` – Pav Sidhu Sep 26 '15 at 20:46

4 Answers4

22

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.

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.

Kelly Keller-Heikkila
  • 2,544
  • 6
  • 23
  • 35
  • I had the same issue, and your solution worked, but I am confused on one matter. Actually, I accidentally run the same migrate command twice, so in my migrations/versions folder, two same migrations were created, the first one actually reflected the changes on my model, the second one was blank. Do you think I got this issue because of this? Because when I checked alembic_version the number matched the first migration but not the second – Humoyun Ahmad Jan 18 '17 at 04:31
  • It shouldn't cause a problem, since if you open the second migration script you'll see it doesn't actually do anything. If you're OCD (like me), you can delete the second migration file that's not needed. Just make sure the value of `alembic_version.version_num` matches the filename of the first (correct) migration script. – Kelly Keller-Heikkila Jan 18 '17 at 14:17
  • This approach eventually worked for me. My alembic_versions table was empty, so I went ahead and manually inserted the value of the latest migration file name and the migrate command worked :) – kip2 May 02 '18 at 15:19
  • I am having difficulty trying to reproduce this solution. Locally, I do not have this issue `ERROR [root] Error: Can't locate revision identified by '...`. However, any attempts to upgrade tables on Heroku after I deploy the app is futile. I have deleted the migrations folder and the `app.db` file and run `init`, `migrate` and `upgrade` all over again, but I cannot solve the heroku error. – Gitau Harrison Nov 19 '20 at 14:28
19

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.

Taehan Stott
  • 479
  • 3
  • 10
14

alembic.util.CommandError: Target database is not up to date.

Could you try the 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

user
  • 1,220
  • 1
  • 12
  • 31
0

The first step is remove the latest migrate version created, then you should use these commands:

 flask db stamp head
 flask db migrate -m "newMigration"
 flask db upgrade
edbighead
  • 5,607
  • 5
  • 29
  • 35