Database migration in Django project - python

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.

Related

Migrate command not transforming not creating table in database. Django

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>

Django migrations not applying after moving from test PC to production server/DB

I am unable to run the inital migrations in my django project on my production server.
I started working on my Django app on my computer with a sqlite db. I was able to run the initial migrations (auth, admin, etc.) and they created tables in my sqlite db with no problem. I was also able to get tables created for the models in my app.
Today I moved the same django project to my webfaction server via git and I am unable to get my MySQL db populated with tables at all. As soon as I created the db, I ran migrate but it said there were no migrations to apply. I tried deleting the migrations folder in my app, but that didn't help at all, probably because there is no django_migrations table to compare any migrations against.
It seems as if the initial migrations are stored where django is installed. This is not part of my git repo, so anything there that might indicate whether the migrations have been made wouldn't be pulled over.
I tried running python manage.py migrate admin
And the same thing with auth, but it did't work
I'm not really sure what the proper way is to go about getting these initial migrations to run again so that I have the proper auth and admin tables in my db. Any help would be appreciated.
This is Django 2.1.7 and Python 3.5
Thanks, Yongjin and Nagesh. By asking me to post my DB settings, you made me realize that I hadn't changed the name of the DB from the project I copied the settings from. Much appreciated!

How to restore Django project with pg_dump file?

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.

Django model changes cannot be migrated after a Git pull

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

Django - Migrate command says that table exists after second makemigrations was executed

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

Categories