PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, April 13, 2022

[FIXED] How to transfer data from old database to new modified database in django?

 April 13, 2022     django, django-3.0, migration, postgresql, python-3.x     No comments   

Issue

I have old django project and new django project. I created dump file from database of old django. And also I made changes in tables and created new tables.

Now I want to load that dump file to my new django app. I am facing errors when I firstly migrate then restore data or firstly restore then migrate.. When I do migration first, it says tables already exist.

When I do restore first , it says django.db.utils.ProgrammingError: relation "django_content_type" already exists I use migrate --fake error goes but new tables are not created in database.

I spent 3-4 days but could not succeed.

Please, help me if you can.

PS: my database is postgresql


Solution

This is not straightforward and will need some manual interventions and it depends on what do you want to do in the future

  • If the tables that already exist in the database have a stable design and won't be changed or you can do the changes manually using SQL statements then set managed = False to the models' meta, this will make Django skip making migrations for those models

  • If you want to keep the power of migration in the new project for all models then this will more complex

    1. Delete all your migrations
    2. You need to make your models equivalent to your database, you can set managed=False for new models like Users
    3. Run python manage.py makemigrations, this will create the structure of the initial database.
    4. Fake running the migrations python manage.py migrate --fake
    5. Dump the records of django_migrations table
    6. Create a new empty migration (with --empty) and add the SQL statements of the django_migrations table to it using migrations.RunSQL()
    7. now fake again so you skip that new migration.
    8. Now you are ready to use migrations as usual.

When installing new database, you will just need to run python manage.py migrate



Answered By - Mohamed ElKalioby
Answer Checked By - David Marino (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing