I created a model and migrated the changes to database and inserted some values in it. But at one point I accidentally deleted that table from database using pgAdmin 4 not from django I was trying to empty the table but accidentally deleted it. The model is still in django and I decided to migrate changes again so that that I could my table back in database. After running migrate it says that no migrations to apply but when I look in the database the table is not there.
I tried creating different models in the same app and tried migrating them they migrated but the model with the name same as model that I deleted is not migrating to the database.
I have deleted all the migrations and it still not taking it.
I think it is still somewhere in the django system from where it should be deleted or something.
Any help how can I get that same name model migrated to the database.
Follow 4 below steps:
Delete the rows related to your app from django_migrations table in your database:
DELETE FROM django_migrations WHERE app='<app_name>';
Go to your app folder and delete migration folder.
Make migration:
py manage.py makemigrations <app_name>
And then migrate:
py manage.py migrate <app_name>
Related
The database is postgres and framework is django.
Running migrations works fine on localhost but not on Heroku server. I cannot drop or reset the database because it contains important data. I backed up the database, reset it and all migrations worked, but when I restored the backup, it reverted back to the old migration state and the problem persisted.
I don't know how to transfer only the data from the backup into a reset database instead of also transferring the old database structure.
When I run heroku run python manage.py migrate I get this error:
No migrations to apply.
Your models in app(s): 'app1', 'app2', 'app3', 'app4', 'users' have changes that are not yet reflected in a migration, and so won't be applied.
Run 'manage.py makemigrations' to make new migrations, and then re-run 'manage.py migrate' to apply them.
Using heroku run python manage.py makemigrations app-name does not help either.
Is there a way to map and transfer just the user data from the old database into the new one? Or is there a way to brute force the old database to accept the new migration structure?
I would be glad if I get some help. Thanks.
What is the procedure to restore a Django project using an already restored database from a PostgreSQL pg_dump. All django source code also exist. Will Django migration safe?
If your dump has create table statements and contains all django tables, you can restore it directly onto an empty database. Django will know the status of the migrations as they are stored in a table in the DB.
So the steps would be:
Drop and recreate DB.
If you now run python manage.py showmigrations all migrations will appear unapplied
Restore DB from dump
If you now run python manage.py showmigrations now, the corresponding migrations will appear applied. If your django project has new migrations that weren't applied when the dump was created they will appear unapplied.
And that's it! Now you can apply the new migrations if there are any and keep working on the Django project.
I am working on a django project. I have this database on my system which has 10,000 of data. I made the project and in settings added that postgres database. Then I did a migration using ./manage.py makemigrations and ./manage.py migrate and I got all the tables linked to my django project. I found out through some resource that during migration only the tables are migrated the data isn't. But I need all the data for that database. Any leads on how I can do that?
PS: I am new to django, so excuse.
I am working on a Django project with another developer. I had initially created a model which I had migrated and was synced correctly with a MySQL database.
The other developer had later pulled the code I had written so far from the repository and added some additional fields to my model.
When I pulled through his changes to my local machine the model had his changes, and additionly a second migration file had been pulled.
So I then executed the migration commands:
python manage.py makemigrations myapp, then python manage.py migrate in order to update my database schema. The response was that no changes had been made.
I tried removing the migration folder in my app and running the commands again. A new migrations folder had been generated and again my database schema had not been updated.
Is there something I am missing here? I thought that any changes to model can simply be migrated to alter the database schema.
Any help would be greatly appreciated. (Using Django version 1.9).
After you pull, do not delete the migrations file or folder. Simply just do python manage.py migrate. Even after this there is no change in database schema then open the migrations file which came through the git pull and remove the migration code of the model whose table is not being created in the database. Then do makemigrations and migrate. I had this same problem. This worked for me.
In your ProcFile add this line at the beginning.
release: python manage.py migrate
I have one model with the relevant fields setup. I ran manage.py migrate then it created the a table in my database. I then ran manage.py makemigrations so that it could create an initial migration file with prefix '0001'. I added another field to my model and then ran manage.py makemigrations again, which created another migration file with prefix '0002'. When I run manage.py migrate now it still looks at the first migration file and so throws out an error that the 'Table already exists'. How can I make it only look at the latest migration file so that it adds the new column to the table? I'm using MySQL.
This will never happen unless django detects (thinks) that the database has not been setup, and tries to initialise the tables with a schema.
Looks like your tables are already setup, but this is not known to django. That is why it tries to start applying the first migration - the table creation and schema is included in that.
You can use the --fake and --fake-initial options as per your specific problem, which tell django that the tables are already setup and ready, and to fake the migrations.
Useful links for more info:
django migrations - django documentation
django migrations, a primer - realpython
how to redo a migration after fake - stackoverflow