B
B
bimka2021-12-21 20:43:12
Python
bimka, 2021-12-21 20:43:12

How to rollback a migration so that no ValueError occurs?

Added the owner attribute with the settings.AUTH_USER_MODEL foreign key to the Jokes class in models.py.
During migration, the console prompted to fill in the attributes for existing objects.
I entered the fill as "user2", but it was necessary to link through the user id.
Now when migrating, it throws an error ValueError: Field 'id' expected a number but got 'user2'.
Tried to rollback via python manage.py migrate blog zero but still gives the same error.
How can I roll back the migration so that this error does not occur?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
shurshur, 2021-12-22
@shurshur

It's not so easy. If something categorically went wrong, then you need to manually correct the database. Or roll back the database neatly to the previous state (for example, delete new fields in the tables), delete the row about the applied migration from the django_migrations table, delete this very failed migration from APP_NAME/migrations, then make makemigrations again and roll the resulting migration.
To the heap:
manage.py showmigrations - will show all migrations in all applications
manage.py sqlmigrate APP_NAME MIGRATION_NAME - will show SQL queries corresponding to this migration

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question