By continuing to navigate on this website, you accept the use of cookies to serve you more relevant services & content.
For more information and to change the setting of cookies on your computer, please read our Cookie Policy.

How to Create initial django migrations for existing DB schema.

Django provides the comfort database migrations from its version 1.8, with which we can avoid the usage of third party packages like the south. Adding migrations to new apps is straightforward - they come preconfigured to accept migrations, and so just run make migrations once you’ve made some changes. But if your app already has models and database tables, and doesn’t have migrations yet (for example, you created it against a previous Django version)  or you got your migrations messed up, you’ll need to convert your app to use migrations. Following are the steps to create initial migrations to your app:

Step1: Empty the django_migrations table: 

Just go to your corresponding database terminals and delete all the records from you django_migrations table with

delete from django_migrations;

Step2: Remove all the files in migrations folders in each and every app of your project.

Go to terminal and run remove all files in migrations folder with 

rm -rf <app>/migrations/

Step3: Reset the migrations for the "built-in" apps:

Reset all the migrations of the Django's built-in apps like admin with the command

python manage.py migrate --fake

Step4: Create initial migrations for each and every app:

For each app run:

python manage.py makemigrations <app>.
Note: Take care of dependencies (models with ForeignKey's should run after their parent model).

Step5: Final step is to create fake initial migrations:

To create initial fake migrations just run 

python manage.py migrate --fake-initial

With all the above five steps all the initial migrations will be created for the existing database schema. Now you can use the Django's migrations system normally. To test if the migrations are succeeded or not, just add a new field to any of the models and run python manage.py make migrations and then it will create a migration file in the corresponding migrations folder of the corresponding app, and then run python manage.py migrate. If this succeeds our above steps are the success and you can enjoy the beauty of Django's migrations.

To Know more about our Django CRM(Customer Relationship Management) Open Source Package. Check Code

    Posted On
  • 22 January 2016
  • By
  • Micropyramid

Need any Help in your Project?Let's Talk

Latest Comments
Related Articles
Preserve file names with sorl for better SEO

We use sorl-thumbnail for scaling images by keeping the original one intact. Sorl proven to be great tool for generating different sized images throughout website. ...

Continue Reading...
Get count, average, min, max values from model field using Django Aggregate

To retrieve maximum, minimum or average values from group of rows we can use django Aggregation. For example to retrieve max price or avg price ...

Continue Reading...
Check test coverage in Django code with Coveralls

Coverage: It is a tool used for measuring the effectiveness of tests, showing the percentage of your codebase covered by tests.
Test Coverage is an ...

Continue Reading...
open source packages

Subscribe To our news letter

Subscribe and Stay Updated about our Webinars, news and articles on Django, Python, Machine Learning, Amazon Web Services, DevOps, Salesforce, ReactJS, AngularJS, React Native.
* We don't provide your email contact details to any third parties