Django migration not applied to the DB - python

I had an Django2.2.3 app, it was working fine. But I had to chane the name of a field in a table, and add another field. Then I ran ./manage.py makemigrations && ./manage.py migrate. Besides the terminal prompt:
Running migrations:
No migrations to apply.
No error is throwed. But then when I go to the MySQLWorkbench to check the database, it is exactly as I didn't make any change. I tried deleting the migrations and making again, the process ends with no errors but the database don't change. I create another empty database, change the name on settings.py and make migrations and migrate again, and it worked, but when I put the old database name on the settings, it just did not work.
Can someone explain this behavior for me? There is any kind of cache for these information migrations or something? I realy want to know why this is not winrkig as I espect.

Make sure the app with the migrations is in the INSTALLED_APPS. Django won't look at the app for changes otherwise.

Adding new few fields to an existing model (table) is one reason for this problem. A way to go about this is simply as follows:
a) un-apply the migrations for that app:
python3 manage.py migrate --fake <app-name> zero
b) migrate the required migrations (you've already deleted previous migrations and you've done 'makemigrations' for the newly added column. So, you just migrate:
python3 manage.py migrate <app-name>
If the steps above didn't solve the problems, then drop the table first;
i) python3 manage.py dbshell
ii) DROP TABLE appname_tablename
close the shell and repeat a and b again.

Related

How could I rework Django migrations without affecting database? [duplicate]

I changed a field on a model class (which has no classes point to it, only one foreign key pointing out of it). Somehow, this stuffed up my migrations and it keeps saying "django.db.migrations.graph.NodeNotFoundError:" looking for migration files that do not exist.
I accidentally deleted several files in my 'migrations' folder.
My database contains a lot of data, and I do not want to break it.
Will I lose any data if I:
Remove the table that caused the problem in the first place (psql, \d, DROP TABLE tablename)
delete all my migration files
Re run the migration from the start?
./manage.py makemigrations
./manage.py migrate
Can anyone recommend another way of fixing this?
Here is the traceback:
http://dpaste.com/0Y1YDXS
Aren't you using git so that you can get your migration files back? If not, install and use it, starting now
I would suggest:
make a backup/dump of your database first, in case something goes wrong
Delete all migrations
Empty migration table in psql
call makemigrations
call migrate --fake-initial
Empty the django_migrations table:
delete from django_migrations;
Remove all the files in migrations folders in each and every app of your project.
Reset the migrations for the "built-in" apps:
python manage.py migrate --fake
Create initial migrations for each and every app:
python manage.py makemigrations your_app_name
Final step is to create fake initial migrations:
python manage.py migrate --fake-initial
See here https://micropyramid.com/blog/how-to-create-initial-django-migrations-for-existing-schema/

Django - can't update model in database

I have a table in my database (postgresql) that is associated with a model called feedback in my Django app (which is also called feedback).
I deleted a couple of columns of the feedback model in the models.py file and then created a migration using:
python manage.py makemigrations feedback
And then tried to "merge" it with my database using:
python manage.py migrate feedback
But i got the error:
django.db.utils.ProgrammingError: relation "feedback" already exists
Of course it exists, but i want to apply the changes i've made. In the migrations folder i have the following files:
__init__.py
0001_initial.py
0002_remove_feedback_created_on.py
0003_remove_feedback_is_read.py
The last one contains my most recent changes. What should i do?
Usually this relation x already exists problem appears when you've applied the change at some point in the database, but the Django migrations system hasn't got a clue about the fact that you already did that. How could it happen? For example if you add a ForeignKey field to your model, make a migration, migrate it, but then change your mind and delete the migration without migrating backwards before that. Nothing breaks in the project, but the relationship and the field in the database remain (you might run into trouble, if the field is non-nullable). Now if you change your mind again and decide that shiiit, I needed that field anyhows and add it again, make a new migration and try to migrate it, then you get the relation x already exists error.
Ok, what then? Usually the simplest way is to fake the failing migration, but BEWARE that if you have any other unapplied changes in the migrations, then those won't be applied to the database either. Before you start faking migrations, make sure that you understand what and why you are faking. Ideally you'd have only one AddField in the migration file that is soon to be faked, because otherwise all changes in the migration file will be faked (faking migrations means, that the changes are not applied to database, but Django will consider them done).
If you have two unapplied migrations, then you definitely shouldn't run migrate --fake without specifying the migration number, because then the latter unapplied migration will be unapplied as well. It SEEMS in your case, that running manage.py migrate 0002 --fake and manage.py migrate afterwards might fix everything, but without knowing everything about what you've done, it's impossible to say for certain.
If I picked up your project, here's what I would do:
1) Check if 0002 contains more than one change.
If yes, then copy everything except the failing field to 0003.
If no, then proceed to manage.py migrate 0002 --fake.
2) Run manage.py migrate again to migrate 0003.
It could also help in the case of migrations.
python3 manage.py migrate --run-syncdb

Django migration issue.

I created a django application so along the line I wanted to reset the entire database so I deleted the database and deleted all migration files. it is not the first time I have done such which I assume is not a bad way of doing it. so I ran the command like python manage.py makemigrations and I got this error in my terminal
django.db.migrations.exceptions.NodeNotFoundError:
Migration auth.0009_user_following dependencies reference nonexistent parent node (u'profiles', u'0001_initial')
I am totally confused and I dont have any idea on what to do next. HELP
You have a custom migrations in your generic app auth, just delete the file:
VIRTUALENV_PATH/lib/python2.7/site-packages/django/contrib/auth/migrations/auth.0009_user_following
You need to run this to get the initial migration:
python manage.py makemigrations <nameofyourapp>
Then run
python manage.py migrate

makemigrations does not work after deleting migrations

I mistakenly deleted all the .py file under path projectName/appName/migrations, it include like:
0001_initial.py
0011_auto_20150918_0723.py
0002_auto_20150819_1301.py
...
Now, even I updated the Model file, and run the command python manage.py makemigrations, it always prompt with No changes detected.
How can I recover everything?
First of all, even if this happened to you in production, do not panic.
When you deleted all migrations django forgot that this app is supposed to be managed by migrations. Django defaults back to the legacy python manage.py syncdb migrationless behaviour and will not attempt to detect changes or generate new migrations when you run python manage.py makemigrations
In order to make it aware of the migrations, you have to run the command specifically for your app:
python manage.py makemigrations appName
However, for your running application, django will not be able to detect that the new migrations were already applied in the database, and will try to run them again when you run python manage.py migrate.
When this happens the migrations fail saying that the relation appName.XYZ already exists!.
To make django understand that your migration is already reflected in the database you have to fake them:
python manage.py migrate appName --fake
This will update the migration history table and mark your migrations as applied, but will not attempt to create the tables in the database.
Update (thanks to #GwynBleiD):
One concern here is that deleted migrations which were already applied, will be listed in the migrations history table in the database. This will not be a problem for the initial python manage.py makemigrations myApp, however for any other migrations it will raise an error about inconsistent migration history.
To avoid that, you must remove by hand any row from django_migrations table in database that refers to nonexistent migrations.

Updating django application, migrations

Is it possible to remove schemamigrations from the Queue? For example. When I run the command ./manage.py migrate --list, it returns a list of all migrations including items that have yet to be migrated.
I have 5 items that have not been migrated yet, but I do not want to migrate 2 of them because I know they will throw an error.
Is is possible to remove those migrations from the 'queue' so to speak?
Thanks!
Remove the corresponding files from yourapp/migrations. There are all files stored after you called
python manage.py makemigrations
These files are named like 0001_initial.py.
If there is no file, nothing happens when you call
python manage.py migrate
When your model is ready, you can create new migrations for your database.
You should use the --fake option when you run migrate to mark those migrations as applied.

Categories

Resources