Django migration issue. - python

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

Related

django.db.utils.OperationalError: no such table: blog_category

I keep getting the error when i try running python3 manage.py makemigrations and python3 manage.py migrate
i have checked over a lot of posts and they all say to run python3 manage.py makemigrations and python3 manage.py migrate or even python3 manage.py migrate --run-syncdb but the problem is that i get this error when i run those commands as well as when i run python3 manage.py runserver:
return Database.Cursor.execute(self, query, params)
django.db.utils.OperationalError: no such table: blog_category
the other problem is that i don't every have any code that says blog_category so i don't even know where django is getting that from. i'v tried different venv's to see if other installed programs were the problem but the error persists. Please help.
I had this problem too. I was unable to make new migrations even after manually deleting the migrations. I noticed that my db.sqlite3 file turned into a text file after forking my project from github.
Here's what you can do:
Ensure that your db.sqlite3 file in your project root folder has the extension ".sqlite3", it should not be without a file extension. Also make sure that it is not an empty file (0 KB).
You should not be able to open the db file and see this:
This is not what should be. The way around fixing such an issue is to copy the db.sqlite3 file over from another project (as it will keep generating a false one otherwise). Then make some changes to admin.py or models.py so that the database will realize it needs to do something (make migrations), and then luckily - you should now be able to make migrations (python manage.py makemigrations).
If the above does not provide resolution, this is how you can completely reset the database:
python manage.py flush
Create the superuser again and make any necessary migrations:
python manage.py createsuperuser
python manage.py makemigrations
python manage.py migrate
It is also important to ensure that you have no active code in your project which would try to query data from a database table which no longer exists.
Realize that you now have an empty database.
If I would have code running in the project that would try to retrieve data from a database object that used to be there in the past, something like:
p = Category.objects.get(title=flower_type)
print(p.flower_set.first())
print(p.flower_set.last())
It would cause problems, since there are no database entries, and there is no data to query. It's something to watch out for.

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 migration not applied to the DB

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.

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