Migrations in DJango - python

I am having DJango migrations problem while making migration following error is coming.
When I run my applications using python manage.py runserver it shows this :-
However, running python manage.py makemigrations shows no changes detected
And Above three images are result after running python manage.py migrate.
What is the problem with this?

When the *table> already exists Error happens, it is usually due to deleting and rerunning the initial migration or models.py file. For these scenarios,
python manage.py makemigrations <app_name>
python manage.py migrate --fake-initial <app_name>
Or if you want to fake only one migration file
python manage.py migrate <migration_file_number> --fake <app_name>
--fake-initial tells Django to mark initial migration as migrated without actually running its corresponding SQL.
Django's migration document may be helpful

looks like you changed the database or migration files manually.
try to re-create the database.
delete the DB file
delete all migrations files (keep the init file)
run create migrations command
run migrate command

Related

Django Makemigrations not working in version 1.10 after adding new table

I added some table models in models.py for the first time running the app and then ran python manage.py makemigrations followed by python manage.py migrate. This works well but after adding two more tables it doesn't work again.
It created migrations for the changes made but when I run python manage.py migrate nothing happens. My new tables are not added to the database.
Things I have done:
Deleted all files in migrations folder and then run python manage.py makemigrationsfollowed by python manage.py migrate but the new tables are not still not getting added to the database even though the new table models show in the migration that was created i.e 0001_initial.py.
Deleted the database followed by the steps in 1 above but it still didn't solve my problem. Only the first set of tables get created.
Tried python manage.py makemigrations app_name but it still didn't help.
I have run into this problem before and found that running manage.py for specific tables in this fashion worked:
python manage.py schemamigration mytablename --auto
python manage.py migrate
Also make sure that your new table is listed under INSTALLED_APPS in your settings.py.
Can you post your models?
Have you edited manage.py in any way?
Try deleting the migrations and the database again after ensuring that your models are valid, then run manage.py makemigrations appname and then manage.py migrate.
You could try:
Deleted everything in the django-migrations table.
Deleted all files in migrations folder and then run python manage.py makemigrations followed by python manage.py migrate as you said.
If this doesn't work, try:
Deleted everything in the django-migrations table.
Deleted all files in migrations folder, use your old model.py to run python manage.py makemigrations followed by python manage.py migrate.
Add new model, run python manage.py makemigrations followed by python manage.py migrate again.

Reset SQLite database in Django

I am trying to refactor a Django project. I renamed a couple apps and added a new one, as well as shuffled some models around. I want to clear my database and migrations and start fresh, but I am not sure how to accomplish this. Here's what I did:
rm -r myapp/migrations // I ran this for all my apps
python manage.py flush
python manage.py makemigrations myapp // I ran this for all my apps
python manage.py migrate // This errors
I get an error:
django.db.utils.OperationalError: table "myapp_mymodel" already exists
Can anyone tell me what I might be doing wrong?
EDIT: What is the django command to delete all tables? did not work.
Delete database and delete migration files (.py and .pyc) in migrations directory of your app (don't delete __init__.py file). Then run python manage.py makemigrations app and python manage.py migrate.
I had the same issue, using Django 1.10, here is what I did, I deleted the database sqlite file, deleted the pycache folders inside each of the apps, deleted all files inside the migrations folder for each app , except the init.py file, and then ran python manage.py makemigrations and python manage.py migrate. Also note that because you deleted the database you will have to create a new superuser using python manage.py createsuperuser. Hope this helps
For me, just
python manage.py flush
deleted old db contents, so i was able to create records anew in Django 2.1.4.
Don't forget to create new superuser:
python manage.py createsuperuser
This may help you if you want to clear sqlite3 DB follow these steps.
Delete migrations files except init.py
Delete dbsqlit3 file
Then type python/python3 manage.py migrate
Then make changes in your models
Type python/python3 manage.py makemigrations
Type python/python3 manage.py migrate
Then you have to create new superuser by just typing python/python3 manage.py createsuperuser . you should use new name not old user name
Do not delete your database file!
Its correct to delete migration files and then run flush but deleting sqlite database file is wrong. This worked to me every time. If you are using other database, it will save you a lot of work and preparations.
delete all ".py" and ".pyc" files manually
python manage.py flush
type "yes" to confirm
python manage.py makemigrations
python manage.py migrate

How to recreate a deleted table with Django Migrations?

There are two models Groups and Students and only one table for Groups of them, the Students table was deleted.
How to make Django recreate the deleted table? If I do makemigrations it prints "No changes detected".
On admin page when I click on the Students table it throws an exception:
relation "students_students" does not exist
In django 1.7 you can try:
1. Delete your migrations folder
2. In the database: DELETE FROM django_migrations WHERE app = 'app_name'.
You could alternatively just truncate this table.
3. python manage.py makemigrations
4. python manage.py migrate --fake
If you are working in django 1.9.5 this is the 100 % solution for this problem:
1. Delete your migrations folder
2. In the database: DELETE FROM django_migrations WHERE app = 'app_name'.
You could alternatively just truncate this table.
3. python manage.py makemigrations app_name
4. python manage.py migrate
This works 100% for me!
Jan 2021
I had a migration problem and I had to drop/delete a table by pgadmin. Then, when I makemigrations and migrate the table wasn't recreated. In this way, I've found this procedure which worked for me:
python manage.py migrate --fake app_name zero
python manage.py migrate app_name
[NOTE]
If you don't have the intended migration file, create that before the above commands by python manage.py makemigrations
If you don't want to roll back to the initial(zero) state use the number of migration file instead of zero, e.g. python manage.py migrate --fake myappname 0005
I tested this approach in Django 2.2
Read More
There isn't an easy way to get Django to recreate a table that you have deleted manually. Once your database is altered manually, Django's view of the database (from migrations) is different from reality, and it can be tricky to fix.
If you run the sqlmigrate command, it will show you the required SQL to create the table. You can run the sql in a database shell. Assuming your app name is students, and the migration that created the table was 00XX_create_students.py, you would do:
./manage.py sqlmigrate students 00XX_create_students
Be careful if there are foreign keys to or from the students table, the constraints will have to be created as well.
The only way that worked for me:
rm -r <app-name>/migrations/
python manage.py makemigrations <app-name>
python manage.py sqlmigrate <app-name> 0001_initial
Copy what it prints out (or, depending on what you have actually removed from the DB, only part of the SQL queries).
Apply those copied queries to your DB:
psql -U user_name -h 127.0.0.1 database_name
Paste what you have copied from the SQL queries printout.
Commit the queries.
And that's it - your missing tables are created.
The answer that worked for me is as follows:
Assume in your database a table of a model has been deleted and you need to re-create, then do the following.
comment out the model in models.py that creates the table that has been deleted (either the model class or a line that creates a table like a = models.ManyToManyField(...))
run: python manage.py makemigrations <app-name>, where <app-name> is the name of of the app where you have models.py
run: python manage.py migrate --fake <app-name>
un-comment the model in models.py
run: python manage.py makemigrations <app-name>
run: python manage.py migrate <app-name> (without the --fake)
and you the table should be back in the database. But any data that was in the table will be lost.
Delete the migration folder from your migration app folder and simply run the migration commands:
python3 manage.py makemigrations appname
python3 manage.py migrate
I just deleted my migrations folder, dropped the whole database, then i made migration for the app
python3 manage.py makemigration
python3 manage.py migrate
and it came back.
Rename the deleted table name to some_new_name in the models.py and run:
python3 manage.py makemigration
python3 manage.py migrate
again rename the some_new_name table to the original name and run
python3 manage.py makemigration
python3 manage.py migrate
finally, go to the dbshell and drop the table some_new_name
I create table manualy and it helps.
For Django 1.10.4
I deleted the db.sqlite3 file from the project folder and then ran the following commands:
python manage.py makemigrations app_name
python manage.py migrate
Django 1.11.2 using MariaDB, accidental drop of database table.
To recreate table, try the following:
1/ Delete all except for init.py in your app/migrations directory
2/ select * from django_migrations; delete from django_migrations where app = 'yourapp';
3/ Check your model is good and run: python manage.py makemigrations
4/ python manage.py migrate
Works for me!
if you have created your classes and performed the migration operation, and then you want to add items to your classes, empty the migration folder with this command beforehand.
In Django 3, I proceeded according to the following steps and it worked 100%
python manage.py makemigrations appname --empty
python manage.py makemigrations appname
python manage.py migrate
Actually, the above methods did not work for me, so I just perform the below workaround as I did not want to manually write the whole query to create the table.
So I changed the database in the settings file and re-ran the migrations command after deleting the migrations folder, then just performed the python migrate command it created new tables in another database then from there just opened the table in query view, copied the script, and inserted the table in my main database.
Another Simple way to do this is
Go to your migrations folder.
Search for the file which contains the code to create the Students table in DB.
Remove the code snippet from the file and save it.
Then run py manage.py makemigrations and py manage.py migrate again
This worked for me :)
In this case, you need to trick django!
Do one thing...
copy the "students" model in models.py with other name like
"studentscopy".
Now run --> python manage.py makemigration
It will create a new migration in migration package of your app. Open
that last migration and rename "studentscopy" back to "students"
in that file.
Now run --> python manage.py migrate
It will create the table again with "students" name and at last delete that "studentscopy" model from your models.py file.
Below steps solved the problem for me
Delete all migrations files
python manage.py makemigrations (Create your initial migration file 0001_inital.py)
python manage.py migrate --fake <app_name> zero
( Tell Django to mark the migrations as having been applied or unapplied, but without actually running the SQL to change your database schema. This brings the migrations to zeroth state. Otherwise Django will think there's no change to apply )
python manage.py migrate

How to apply a particular django database migration in heroku?

I uploaded my modified code with some changes in models. When I run heroku run python manage.py migrate app to apply the database migrations it gave me an error
CommandError: Conflicting migrations detected (0004_auto_20150819_0827, 0008_auto_20150813_1444 in app).
To fix them run 'python manage.py makemigrations --merge'
So when I run heroku run python manage.py makemigrations --merge it gave me output:
Created new merge migration /app/app/migrations/0009_merge.py
Now how can apply this migration to my database ?
Maybe
heroku run python manage.py migrate
or to see what it's going to apply:
heroku run python manage.py showmigrations

South Migration Error

I am trying to learn Django by setting up one of the projects I found on github. Afetr I ran the syncdb command it showed
Not synced (use migrations):
- django_extensions
- djangoratings
- profiles
- guardian
(use ./manage.py migrate to migrate these).
When I am running "python manage.py migrate app" , it gives
AttributeError: 'module' object has no attribute 'Migration'.
I also ran schemamigration app --auto and --initial as well. But nothing seems to be working. Can somebody point out where I am going wrong.
Are you actually running python manage.py migrate app exactly? If you want to migrate all apps, just run python manage.py migrate. (without app) If there is an app actually named app that you want to run migrations for, then you would do what you did.
If you still get the error after running python manage.py migrate, then there must be an invalid migration file somewhere. I would migrate each app individually like this:
python manage.py migrate django_extensions
python manage.py migrate djangoratings
... etc.
to find the app with bad a migration file. Once you find the app, look in the migrations folder of the app to find any empty migration files.
First thing you must run initial migration.
python manage.py schemamigration --initial
You are getting this error because of your project structure or there is some issue with your south package .try to remove and reinstalling south.
You need to see that there should not be any extra .py file in your migration folder.

Categories